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?
main.cpp
sin()
libmath
g++ -o main main.cpp -lmath
libmath.so
libmath.a
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
If your linker supports -l:<filename> , you can use:
-l:<filename>
g++ -o main main.cpp -l:libmath.a
Use this function:
g++ -o main main.cpp /path_to/libmath.a