user150497
user150497

Reputation: 532

Two method for linking a object using GCC?

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?

Upvotes: 3

Views: 242

Answers (2)

Wexxor
Wexxor

Reputation: 1909

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.

Upvotes: 0

Alex B
Alex B

Reputation: 84922

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.

Upvotes: 4

Related Questions