C/C++——linux默认动态库连接、动态库与静态库混合连接-Wl

linux默认对库的连接是使用动态库

在应用程序需要连接外部库的情况下,linux默认对库的连接是使用动态库,在找不到动态库的情况下再选择静态库。

使用方式为:

gcc test.cpp -L. -ltestlib

如果当前目录有两个库libtestlib.so libtestlib.a肯定是连接libtestlib.so

如果要指定为连接静态库则使用:

gcc test.cpp -L. -static -ltestlib

使用静态库进行连接。

对动态库与静态库混合连接 -Wl

当对动态库与静态库混合连接的时候,使用-static会导致所有的库都使用静态连接的方式。这时需要作用-Wl的方式:

gcc test.cpp -L. -Wl,-Bstatic -ltestlib  -Wl,-Bdynamic -ltestdll 

缺省库动态链接模式恢复

另外还要注意系统的运行库使用动态连接的方式,所以当动态库在静态库前面连接时,必须在命令行最后使用动态连接的命令才能正常连接,如:

gcc test.cpp -L. -Wl,-Bdynamic -ltestdll -Wl,-Bstatic -ltestlib  -Wl,-Bdynamic

最后的-Wl,-Bdynamic表示将缺省库链接模式恢复成动态链接

例子

I have a C project that produces ten executables, all of which I would like to be linked in statically. The problem I am facing is that one of these executables uses a 3rd-party library of which only the shared-object version is available.

If I pass the -static flag to gcc, ld will error saying it can’t find the library in question (I presume it’s looking for the .a version) and the executable will not be built. Ideally, I would like to be able to tell ‘ld’ to statically link as much as it can and fail over to the shared object library if a static library cannot be found.

In the interium I tried something like gcc -static -lib1 -lib2 -shared -lib3rdparty foo.c -o foo.exe in hopes that ‘ld’ would statically link in lib1 and lib2 but only have a run-time dependence on lib3rdparty. Unfortunatly, this did not work as I intended; instead the -shared flag overwrote the -static flag and everything was compiled as shared-objects.

Is statically linking an all-or-nothing deal, or is there some way I can mix and match?

解决:

Looking at this thread you can see that it can be done. The guys at GNU suggest

gcc foo.c -Wl,-Bstatic -lbar -lbaz -lqux -Wl,-Bdynamic -lcorge -o foo.exe

例子 2

gcc 同时连接 静态库和动态库现在有 libmy.a & libmy.so两个库,其中的函数供main.cc调用要在可执行文件中同时连接这两个库

试错1

gcc -g -lstdc++ -g -L. -lmy -l ./libmy.a -o test.exe main.cc 

报找不到libmy.a,可是在当前目录下已经有这个文件了

试错2

gcc -g -lstdc++ -g -L. -l libmy.so -l ./libmy.a -o test.exe main.cc

报找不到 libmy.so,

试错3

在当前目录下也有这个文件用了 -static 选项,则报动态库中的函数没定义

解决

静态库混合动态库要加特殊指令的,:

gcc -g -lstdc++ -g -WI,-Bdynamic -L. -lmy -WI,-Bstatic -L. -lmy -o test.exe main.cc

找不到库的处理方案

有两种方法:

  1. 可以把当前路径加入/etc/ld.so.conf中然后运行ldconfig,或者以当前路径为参数运行ldconfig(要有root权限才行)。
  2. 把当前路径加入环境变量LD_LIBRARY_PATH

如何使用库

gcc中关于库的参数有:

-L 指定搜寻库的目录

  如指定当前目录` gcc -L` .

-l 指定要链接的库的名称

加入库的名称是libmylib.a,则gcc -l mylib,即去头去尾。

–static 组织在链接时使用动态库

–shared 生成动态库

--static-libgcc  链接静态libgcc库
--shared-libgcc 链接动态libgcc库

可见对动态库和静态库的使用方法是一样的,同一个库如果同时存在动态库和静态库,优先链接动态库,除非使用--static强制使用静态库。

查看库中的文件按

注意:参数信息只能存在于 .h 头文件中
windows下

dumpbin   /exports   libxxx.a

linux 下

nm   -g   --defined-only   libxxx.a
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值