How can I force a static library to link when there is a shared library with the same name

Suppose I have a main.cpp file that uses the sin() function, which is defined in libmath . Also suppose we have both libmath.a and libmath.so available in the same directory. Now, if I issue the command g++ -o main main.cpp -lmath , the default behavior of Linux is to link to the libmath.so shared library. I want to know if there is a way to get the program to link to the libmath.a static library without deleting or moving the shared library?

+23
gcc linux g ++ ld
Dec 21 2018-10-21
source share
3 answers

You need to pass the -static to the linker, but only for the specific libraries that you want. eg:.

 g++ -o main main.cpp -Wl,-Bstatic -lmath -Wl,-Bdynamic 
+19
Dec 21 2018-10-21
source share

If your linker supports -l:<filename> , you can use:

 g++ -o main main.cpp -l:libmath.a 
+7
Dec 21 '10 at 14:39
source share

Use this function:

 g++ -o main main.cpp /path_to/libmath.a 
+4
Dec 21 '10 at 14:20
source share



All Articles