linux环境下同时使用静态库、动态库编译程序

1. 应用场景

因某些原因,需要同时使用静态库及动态库编译代码。
在这里我需要静态链接的是zbarlib,动态链接的是opencv库。
经过一个下午的艰苦奋斗,分享一下网上的解决方法以及最终导致不成功的原因所在。


2.Makfile实例

CXX=g++

CFLAGS += -I${PWD}/../zbar/include
CFLAGS += -I${PWD}/../opencv/include

LDFLAGS += -Wl,-Bstatic -lzbar -L${PWD}/../zbar/lib 
LDFLAGS += -Wl,-Bdynamic -lpthread -lrt -lopencv_core  -lopencv_highgui  -lopencv_imgproc  -lopencv_ml  -lopencv_video -L${PWD}/../opencv/lib
LDFLAGS += -Wl,--as-needed

objects = main.o
target = main

all:${target}

${target}:$(objects)
    $(CXX) $^ -o $@ ${LDFLAGS}

%.o:%.cpp
    $(CXX) -c ${CFLAGS} $^ -o $@

.PHONY:clean
clean:
    @rm -f  ${target}
    @rm -f  *.o

3.实例分析

可以看到makefile中编译参数使用的是
-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.  

NOTE: In Ubuntu 8.10 and later versions, for LDFLAGS, the option -Wl,-z,relro is used. To disable, use -Wl,-z,norelro.

主要功能就是向链接器传递参数,好像不加这个也可以编译通过。

   -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.

-Bstatic 告诉链接器,链接静态库
-Bdynamic 告诉链接器,链接动态库

 --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.

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


4. 错误

经过一个下午的折腾,发现仅仅是编译语句写得有问题,真是冤啊。

编译出错情况:

${target}:$(objects)
    $(CXX) ${LDFLAGS} $^ -o $@

其中 ${LDFLAGS} 参数放在 $^ -o $@前面,就会出现找不到静态库函数的错误,这里没有时间具体分析,后面有空研究研究。


5. 参考网站

http://blog.csdn.net/wangxvfeng101/article/details/15336955
http://blog.csdn.net/nodeathphoenix/article/details/9058531
http://www.cnblogs.com/little-ant/p/3398885.html

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值