How to link using GCC without -l nor hardcoding path for a library that does not follow the libNAME.so naming convention?

kbluck picture kbluck · Oct 16, 2008 · Viewed 18.5k times · Source

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.

For example:

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?

Answer

David Nehme picture David Nehme · Nov 11, 2008

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

should search your path for the _mylib.so.