linux下同时存在当静态库和动态库同名

Q:linux下同时存在当静态库和动态库同名时,gcc命令会使用哪个库文件呢

A:
gcc会优先选择动态库

# ls
hello.c hello.h main.c
#

在来创建静态库文件libmyhello.a和动态库文件libmyhello.so

# gcc -c hello.c
# ar -cr libmyhello.a hello.o (或-cvr)
# gcc -shared -fPCI -o libmyhello.so hello.o
# ls
hello.c hello.h hello.o libmyhello.a libmyhello.so main.c

通过上述最后一条ls命令,可以发现静态库文件libmyhello.a和动态库文件libmyhello.s
o都已经生成,并都在当前目录中。然后,我们运行gcc命令来使用函数库myhello生成目标
文件hello,并运行程序 hello。

# gcc -o hello main.c -L. –lmyhello (动态库和静态库同时存在时,优先使用动态库, 当然,直接#gcc main.c libmyhello.a -o hello的话,就是指定为静态库了)
# ./hello
./hello: error while loading shared libraries: libmyhello.so: cannot open shar
ed object file: No such file or directory

从程序hello运行的结果中很容易知道,当静态库和动态库同名时,gcc命令将优先使用动态库,默认去连/usr/lib和/lib等目录中的动态库,将文件libmyhello.so复制到目录/usr/lib中即可。
参考:http://hi.baidu.com/%B1%AB%B9%FA%CC%CE/blog/item/e58ed2f142913ea7a50f525e.html

原文地址:https://www.cnblogs.com/moonflow/p/2501480.html