views:

2150

answers:

3

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?

+1  A: 

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
Robert Gamble
A: 

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

Chris Roland
+3  A: 

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.

David Nehme
This, sir, is exactly what I was looking for. Thanks!
kbluck