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

相关文章

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn