对于不遵循 libNAME.so 命名约定的库,如何在没有 -l 或硬编码路径的情况下使用 GCC 链接?

2022-01-11 00:00:00 gcc c shared-libraries linker c++

我有一个共享库,我希望使用 GCC 链接可执行文件.共享库的名称不是 libNAME.so 形式的非标准名称,因此我不能使用通常的 -l 选项.(它恰好也是一个 Python 扩展,因此没有lib"前缀.)

I have a shared library that I wish to link an executable against using GCC. The shared library has a nonstandard name not of the form libNAME.so, so I can not use the usual -l option. (It happens to also be a Python extension, and so has no 'lib' prefix.)

我可以将库文件的路径直接传递到链接命令行,但这会导致库路径被硬编码到可执行文件中.

I am able to pass the path to the library file directly to the link command line, but this causes the library path to be hardcoded into the executable.

例如:

g++ -o build/bin/myapp build/bin/_mylib.so

有没有办法链接到这个库而不会导致路径被硬编码到可执行文件中?

Is there a way to link to this library without causing the path to be hardcoded into the executable?

推荐答案

有一个:"前缀可以让你给你的库起不同的名字.如果你使用

There is the ":" prefix that allows you to give different names to your libraries. If you use

g++ -o build/bin/myapp -l:_mylib.so other_source_files

应该在路径中搜索 _mylib.so.

should search your path for the _mylib.so.

相关文章