求问:关于linux下so文件和-l选项

DoDoMouse 2014-09-03 10:47:39
有几个问题:
1. 如果library path里面或者ld.conf里面同时存在相同名字的静态库和动态库,例如libA.so和libA.a,编译的时候不指定-static,这种情况下,-lA会去link哪个库,是so还是a?为什么?我查询了man手册,找不到。
2. 在创建so文件的时候,会使用类似这种命令:gcc -shared -Wl,-soname=libA.so.1 XX.o -o libA.so.1.1 并用libA.so.1.1创建名为libA.so.1的软链接。我知道加上版本后是跟之后的升级有关,用来控制动态库的多个版本,也知道软链接是为了link时候用的。在readelf的时候,发现在libA.so.1.1中会有libA.so.1的信息,不知道这个信息有什么用?既然已经有软链接了,这个信息是link的时候为了校对的?
3. 如果编译命令中,指定了-lA,那链接器是怎么在library path中或者ld.conf中查找so文件的,会把版本号去掉之后找吗?相关的说明在哪里?
...全文
410 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
DoDoMouse 2014-09-08
  • 打赏
  • 举报
回复
结贴了,根据回帖的正确性给分。
帅得不敢出门 2014-09-04
  • 打赏
  • 举报
回复
arm-linux gcc 如无-static先so后.a 同1楼。
mujiok2003 2014-09-04
  • 打赏
  • 举报
回复
mujiok2003 2014-09-04
  • 打赏
  • 举报
回复
-static On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.
mujiok2003 2014-09-04
  • 打赏
  • 举报
回复
-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.
DoDoMouse 2014-09-04
  • 打赏
  • 举报
回复
引用 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 这是为什么?
熊熊大叔 2014-09-04
  • 打赏
  • 举报
回复
如果-l的名称带版本号,就直接找对应的版本号;如果不带版本号,则在搜索路径下找最新的版本号。
707wk 2014-09-04
  • 打赏
  • 举报
回复
DoDoMouse 2014-09-04
  • 打赏
  • 举报
回复
引用 3 楼 u011761982 的回复:
[quote=引用 1 楼 truelance 的回复:] 1. 不指定-static时,优先用.so
这个在man手册里面有吗?[/quote] 这个在man里面是有的,在ld下面 现在就是想要确定第3点了,link的时候会把版本号去掉然后查找吗?
DoDoMouse 2014-09-04
  • 打赏
  • 举报
回复
引用 1 楼 truelance 的回复:
1. 不指定-static时,优先用.so
这个在man手册里面有吗?
mujiok2003 2014-09-04
  • 打赏
  • 举报
回复
引用 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
DoDoMouse 2014-09-04
  • 打赏
  • 举报
回复
引用 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。并没有说版本号!
熊熊大叔 2014-09-03
  • 打赏
  • 举报
回复
2. 如果.so文件里没有这个信息,那么软连接就都需要手工生成。现在.so文件里有这个信息,执行ldconfig, 就会自动搜索系统目录下的所有.so文件,自动提取这个信息生成软连接。
熊熊大叔 2014-09-03
  • 打赏
  • 举报
回复
1. 不指定-static时,优先用.so

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧