user-0 提问: 求问：关于linux下so文件和-l选项

回复:
user: user-0
content: 结贴了，根据回帖的正确性给分。

user: user-1
content: arm-linux gcc 如无-static先so后.a  同1楼。

user: user-2
content: 具体看看gcc manual

user: user-2
content: -static On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.

user: user-2
content: -llibrary -l library  Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)  It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.   The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.   The directories searched include several standard system directories plus any that you specify with -L.   Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with ‘lib’ and ‘.a’ and searches several directories.

user: user-0
content: 引用 12 楼 mujiok2003 的回复:[quote=引用 11 楼 u011761982 的回复:] 红色那段只是表示会去link liblibrary.so或者liblibrary.a。并没有说版本号！ 那就是不管版本号， 你可以把把版本后放于文件后前，比如library1.1.0.so，然后在link时指定具体版本 -l library1.1.0[/quote] 在centos下验证了，不行。 我有两个文件，文件1是库，文件2是调用库的。 文件1: #include<stdio.h>  void printMyLib1() { 	printf("In printMyLib1()\n"); }  文件2： #include<stdio.h>  void printMyLib1();  int main(int argc, char*argv[]) { 	printMyLib1(); } 文件1(mylib1.cpp)编译为.o文件，然后生成so文件并加上版本号： gcc -shared -fPIC -c mylib1.cpp -o mylib1.o ld -shared mylib1.o -o libmylib.so.1.1 -soname=libmylib.so.1 该文件放置于/home/dodomouse/workspace/Test  文件2(test.cpp)编译的时候link文件1生成的so文件，发现出错: gcc test.cpp -o test -L/home/dodomouse/workspace/Test/ -lmylib /usr/bin/ld: cannot find -lmylib collect2: error: ld returned 1 exit status  如果生成的时候不加版本号，则是ok的： ld -shared mylib1.o -o libmylib.so gcc test.cpp -o test -L/home/dodomouse/workspace/Test/ -lmylib  这是为什么？

user: user-3
content: 如果-l的名称带版本号，就直接找对应的版本号；如果不带版本号，则在搜索路径下找最新的版本号。

user: user-4

user: user-0
content: 引用 3 楼 u011761982 的回复:[quote=引用 1 楼 truelance 的回复:] 1. 不指定-static时，优先用.so 这个在man手册里面有吗？[/quote] 这个在man里面是有的，在ld下面 现在就是想要确定第3点了，link的时候会把版本号去掉然后查找吗？

user: user-0
content: 引用 1 楼 truelance 的回复:1. 不指定-static时，优先用.so 这个在man手册里面有吗？

user: user-2
content: 引用 11 楼 u011761982 的回复:[quote=引用 7 楼 mujiok2003 的回复:] -llibrary -l library  Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)  It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.   The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.   The directories searched include several standard system directories plus any that you specify with -L.   Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with ‘lib’ and ‘.a’ and searches several directories. 红色那段只是表示会去link liblibrary.so或者liblibrary.a。并没有说版本号！[/quote] 那就是不管版本号， 你可以把把版本后放于文件后前，比如library1.1.0.so，然后在link时指定具体版本 -l library1.1.0

user: user-0
content: 引用 7 楼 mujiok2003 的回复:-llibrary -l library  Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)  It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.   The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.   The directories searched include several standard system directories plus any that you specify with -L.   Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with ‘lib’ and ‘.a’ and searches several directories. 红色那段只是表示会去link liblibrary.so或者liblibrary.a。并没有说版本号！

user: user-3
content: 2. 如果.so文件里没有这个信息，那么软连接就都需要手工生成。现在.so文件里有这个信息，执行ldconfig, 就会自动搜索系统目录下的所有.so文件，自动提取这个信息生成软连接。

user: user-3
content: 1. 不指定-static时，优先用.so