40

Suppose I have a file main.cpp which uses sin() function which is defined in libmath. Also suppose that 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 behaviour of Linux is to link to the shared library libmath.so. I want to know is there a way to force the program to link with the static library libmath.a without deleting or moving the shared library?

1

3 Answers 3

33

You'll need to pass the -static to the linker, but only for particular libraries you want. e.g.:

g++ -o main main.cpp -Wl,-Bstatic -lmath -Wl,-Bdynamic
Sign up to request clarification or add additional context in comments.

1 Comment

Surely it shouldn't matter for gcc/binutils, -static and -Bstatic are synonyms in the GNU linker.
15

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

g++ -o main main.cpp -l:libmath.a

Comments

5

Use this function:

g++ -o main main.cpp /path_to/libmath.a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.