Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking a shared library using gcc

I have a shared library (*.so) created using Real View Compiler Tools (RVCT 3.2) on windows target. Then I try to link this *.so file with my application using gcc on linux system.

What is the gcc option to link this shared library with my application linux?

My question is, is the -shared option, which is used as

gcc -shared myfile.so 

..., used to create the SO file or to link the SO file? I believe it creates something like:

gcc -lmyfile.so 

Is this enough? Or is there any other switch to tell the linker that it's a dynamic library (shared object)?

like image 270
goldenmean Avatar asked Apr 07 '09 14:04

goldenmean


People also ask

How do I link a shared library in Makefile?

As already mentioned here, the thing you probably want is the linker option -rpath . Like that, you can set a default search path for the binary. Looks like you even already use -rpath in your makefile, but you specify the wrong path: LIBS = -L$(LIB) -lfuse -lsqlite3 -lkw_taglib -ltag_c -ltag -Wl,-rpath=.

How are shared libraries linked?

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.

Does GCC do linking?

Because of the advantage of dynamic linking, GCC, by default, links to the shared library if it is available. You can list the contents of a library via " nm filename ".


2 Answers

What worked for me was:

gcc -L. -l:myfile.so 
like image 192
mluc Avatar answered Sep 28 '22 08:09

mluc


gcc -lmyfile should be enough (provided that your library is named libmyfile.so). The linker searches for shared objects when possible and AFAIK prefers them.

like image 31
jpalecek Avatar answered Sep 28 '22 10:09

jpalecek