开发者

Two method for linking a object using GCC?

开发者 https://www.devze.com 2023-01-02 16:08 出处:网络
I\'ve known that I should use -l option for liking objects using GCC. that is gcc -o test test.c -L./ -lmy

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 t开发者_JAVA技巧est.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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消