3

I've known that I should use -l option for liking objects using GCC. that is gcc -o test test.c -L./ -lmy

But I found that "gcc -o test2 test.c libmy.so" is working, too.

When I use readelf for those two executable I can't find any difference. Then why people use -l option for linking objects? Does it have any advantage?

2 Answers 2

4

Because you may have either a static or a shared version of the library in your library directory, e. g. libmy.a and libmy.so, or both of them. This is more relevant to system libraries: if you link to libraries in your local build tree, you know which version you build, static or shared, but you may not know other systems' configuration and libraries mix.

In addition to that, some platforms may have different suffixes. So it's better to specify it in a canonical way.

Sign up to request clarification or add additional context in comments.

Comments

0

The main reason is, -lname will search for libname.a (or libname.so, etc.) on the library search list. You can add directories to the library search list with the -L option. It's a convenience built into the compiler driver program, making it easier to find libraries that have been installed in standard places on the system.

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.