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

2024-05-09 02:18

本文主要是介绍linux环境下同时使用静态库、动态库编译程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 应用场景

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


2.Makfile实例

CXX=g++CFLAGS += -I${PWD}/../zbar/include
CFLAGS += -I${PWD}/../opencv/includeLDFLAGS += -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-neededobjects = main.o
target = mainall:${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,optionPass 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_sharedLink 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-staticDo 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-neededThis 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

这篇关于linux环境下同时使用静态库、动态库编译程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/972112

相关文章

Linux配置IP地址的三种实现方式

《Linux配置IP地址的三种实现方式》:本文主要介绍Linux配置IP地址的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录环境RedHat9第一种安装 直接配置网卡文件第二种方式 nmcli(Networkmanager command-line

Java中Runnable和Callable的区别和联系及使用场景

《Java中Runnable和Callable的区别和联系及使用场景》Java多线程有两个重要的接口,Runnable和Callable,分别提供一个run方法和call方法,二者是有较大差异的,本文... 目录一、Runnable使用场景二、Callable的使用场景三、关于Future和FutureTa

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

使用国内镜像源优化pip install下载的方法步骤

《使用国内镜像源优化pipinstall下载的方法步骤》在Python开发中,pip是一个不可或缺的工具,用于安装和管理Python包,然而,由于默认的PyPI服务器位于国外,国内用户在安装依赖时可... 目录引言1. 为什么需要国内镜像源?2. 常用的国内镜像源3. 临时使用国内镜像源4. 永久配置国内镜

Go语言中最便捷的http请求包resty的使用详解

《Go语言中最便捷的http请求包resty的使用详解》go语言虽然自身就有net/http包,但是说实话用起来没那么好用,resty包是go语言中一个非常受欢迎的http请求处理包,下面我们一起来学... 目录安装一、一个简单的get二、带查询参数三、设置请求头、body四、设置表单数据五、处理响应六、超

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.

详解如何使用Python提取视频文件中的音频

《详解如何使用Python提取视频文件中的音频》在多媒体处理中,有时我们需要从视频文件中提取音频,本文为大家整理了几种使用Python编程语言提取视频文件中的音频的方法,大家可以根据需要进行选择... 目录引言代码部分方法扩展引言在多媒体处理中,有时我们需要从视频文件中提取音频,以便进一步处理或分析。本文

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

VSCode配置Anaconda Python环境的实现

《VSCode配置AnacondaPython环境的实现》VisualStudioCode中可以使用Anaconda环境进行Python开发,本文主要介绍了VSCode配置AnacondaPytho... 目录前言一、安装 Visual Studio Code 和 Anaconda二、创建或激活 conda