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

C++CGccLinkerShared Libraries

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

If you can copy the shared library to the working directory when g++ is invoked then this should work:

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

Solution 3 - C++

If you are on Unix or Linux I think you can create a symbolic link to the library in the directory you want the library.

For example:
ln -s build/bin/_mylib.so build/bin/lib_mylib.so

You could then use -l_mylib

http://en.wikipedia.org/wiki/Symbolic_link

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkbluckView Question on Stackoverflow
Solution 1 - C++David NehmeView Answer on Stackoverflow
Solution 2 - C++Robert GambleView Answer on Stackoverflow
Solution 3 - C++Chris RolandView Answer on Stackoverflow