linux使用静态库编译过程,linux环境下同时使用静态库、动态库编译程序 | 码农部落...

因某些原因,有时会需要同时使用静态库及动态库编译代码,这时候我们就必须要了解些下面几个编译参数了。

编译参数

-Wl,-Bstatic

-Wl,-Bdynamic

-Wl,–as-needed

参数定义

-Wl,option

Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas. You can use this syntax to pass an argument to the option. For example, -Wl,-Map,output.map passes -Map output.map to the linker. When using the GNU linker, you can also get the same effect with -Wl,-Map=output.map.

-Wl,-Bstatic

-Wl,-Bdynamic

-Bdynamic

-dy

-call_shared

Link against dynamic libraries. This is only meaningful on platforms for which shared libraries are supported. This option is normally the default on such platforms. The different variants of this option are for compatibility with various systems. You may use this option multiple times on the command line: it affects library searching for -l options which follow it.

-Bstatic

-dn

-non_shared

-static

Do not link against shared libraries. This is only meaningful on platforms for which shared libraries are supported. The different variants of this option are for compatibility with various systems. You may use this option multiple times on the command line:

it affects library searching for -l options which follow it. This option also implies --unresolved-symbols=report-all. This option can be used with -shared. Doing so means that a shared library is being created but that all of the library's external references must be resolved by pulling in entries from static libraries.

–as-needed 只给用到的动态库设置DT_NEEDED。

--as-needed

--no-as-needed

This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed option. Normally the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed or not. --as-needed causes a DT_NEEDED tag to only be emitted for a library that at that point in the link satisfies a non-weak undefined symbol reference from a regular object file or, if the library is not found in the DT_NEEDED lists of other libraries, a non-weak undefined symbol reference from another dynamic library. Object files or libraries appearing on the command line after the library in question do not affect whether the library is seen as needed. This is similar to the rules for extraction of object files from archives. --no-as-needed restores the default behaviour.

使用实例

LDFLAGS += -Wl,-Bstatic -lmystaticlib

LDFLAGS += -Wl,-Bdynamic -lpthread -lrt -lmydynamiclib

LDFLAGS += -Wl,--as-needed

常见错误打印

确定指定好了链接库目录,但是始终提示找不到相应的动态库,但是静态库没有问题,可能有如下提示:

arm-linux/bin/ld.bfd: cannot find -lmylib

这种情况在排查没有指定库目录的情况下就需要检查下是不是静态库和动态库混合编译没有指定相应的参数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值