Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 303
kbluck Avatar asked Oct 16 '08 00:10

kbluck


People also ask

What is the path of gcc?

You need to use the which command to locate c compiler binary called gcc. Usually, it is installed in /usr/bin directory.

How do I specify gcc?

Specify the installation directory for the executables called by users (such as gcc and g++ ). The default is exec-prefix /bin . Specify the installation directory for object code libraries and internal data files of GCC. The default is exec-prefix /lib .

How do I find my gcc configuration?

Run gcc --version -v . It will output the configure invocation.

Does gcc include a linker?

GCC uses a separate linker program (called ld.exe ) to perform the linking.


2 Answers

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.

like image 120
David Nehme Avatar answered Oct 02 '22 15:10

David Nehme


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 
like image 26
Robert Gamble Avatar answered Oct 02 '22 16:10

Robert Gamble