Linux gcc/g++链接编译顺序详解
gcc/g++链接时对库的顺序要求
-Ldir
Add directory dir to the list of directories to be searched for -l. -llibrary
-l library
Search the library named library when linking. (The second
alternative with the library as a separate argument is only for POSIX
compliance and is not recommended.) It makes a difference where in the command you write this option;
the linker searches and processes libraries and object files in
the order they are specified. Thus, `foo.o -lz bar.o' searches
library `z' after file foo.o but before bar.o. If bar.o refers to
functions in `z', those functions may not be loaded. The linker searches a standard list of directories for the
library, which is actually a file named liblibrary.a. The linker
then uses this file as if it had been specified precisely by name. The directories searched include several standard system
directories plus any that you specify with -L. Normally the files found this way are library files—archive files
whose members are object files. The linker handles an archive file
by scanning through it for members which define symbols that have
so far been referenced but not defined. But if the file that is
found is an ordinary object file, it is linked in the usual
fashion. The only difference between using an -l option and
specifying a file name is that -l surrounds library with `lib' and
`.a' and searches several directories. 以上来源于gcc手册
对于library的查找
查找需要连接的符号名是从前向后找,根据-L指定的路径顺序查找;不同 目录下的同名的库,只取第一个(从左向右),后面同名库被忽略;
对于符号的查找
从左向右查找,如果是主程序块和静态库,不能定位地址就报错: ‘undefined reference to: xxx’如果是链接成动态库,则假设该符号在load 的
时候地址重定位。如果找不到对应的动态库,则会在load的时候报:“undefined symbol: xxx“这样的错误。
–as-needed对链接动态库的影响
gcc-4.6默认开启ld的–as-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 satisfies an undefined symbol reference from a regular object file or, if the library is not found in
the DT_NEEDED lists of other libraries linked up to that point, an undefined symbol reference from another dynamic
library. --no-as-needed restores the default behaviour. --add-needed
--no-add-needed
These two options have been deprecated because of the similarity of their names to the --as-needed and --no-as-needed
options. They have been replaced by --copy-dt-needed-entries and --no-copy-dt-needed-entries. --copy-dt-needed-entries
--no-copy-dt-needed-entries
This option affects the treatment of dynamic libraries referred to by DT_NEEDED tags inside ELF dynamic libraries
mentioned on the command line. Normally the linker won't add a DT_NEEDED tag to the output binary for each library
mentioned in a DT_NEEDED tag in an input dynamic library. With --copy-dt-needed-entries specified on the command line
however any dynamic libraries that follow it will have their DT_NEEDED entries added. The default behaviour can be
restored with --no-copy-dt-needed-entries. This option also has an effect on the resolution of symbols in dynamic libraries. With --copy-dt-needed-entries
dynamic libraries mentioned on the command line will be recursively searched, following their DT_NEEDED tags to other
libraries, in order to resolve symbols required by the output binary. With the default setting however the searching
of dynamic libraries that follow it will stop with the dynamic library itself. No DT_NEEDED links will be traversed to
resolve symbols.
--以上来源于man手册
--add-needed Set DT_NEEDED tags for DT_NEEDED entries in following dynamic libs
--no-add-needed Do not set DT_NEEDED tags for DT_NEEDED entries in following dynamic libs
--as-needed Only set DT_NEEDED for following dynamic libs if used
--no-as-needed Always set DT_NEEDED for following dynamic libs
as-needed,意思大概是:只给用到的动态库设置DT_NEEDED。
例如:
g++ -shared a.o -ltest1 -lxxx -lrt -o libtest2.so
当链接生成libtest2.so的时候,如果libtest2.so里面用到了libtest1.so,但是没有用到libxxx.so。
当开启–as-needed选项的时候,就不会链接libxxx.so文件
–as-needed就是忽略链接时没有用到的动态库,只将用到的动态库set NEEDED。
常见错误
1.链接主程序模块或者是静态库的时的‘undefined reference to: xxx’
g++ -Wl,--as-needed -ltest1 -lc -lm -ldl -lpthread -L/home/ocaml/lib/ -lrt -o app main.o
假设main.o依赖libtest1.so中的东西。因为gcc对库的顺序要求(gcc编译时,由左向右)和–as-needed选项的开启(因为libtest1.so在main.o的左边,
所以gcc认为没有使用到它,–as-needed将其忽略),ld忽略libtest1.so,定位main.o的符号的时候当然会找不到符号的定义。
所以会出现‘undefined reference to’这个错误!
正确写法是:是:
g++ -Wl,--as-needed main.o -ltest1 -lc -lm -ldl -lpthread -L/home/ocaml/lib/ -lrt -o app
2.编译动态库(shared library)的时候会导致一个比较隐晦的错误
编译出来的动态库的时候没有问题,但是加载的时候有“undefined symbol: xxx”这样的错误。假如像这也链接PyGalaxy.so
g++ -shared -Wl,--as-needed -lGalaxyParser -lc -lm -ldl -lpthread -L/home/ocaml/lib/ -lrt -o PyGalaxy.so PyGalaxy.o
load PyGalaxy.so的时候会有上面的运行时错误!
简单分析原因:因为libGalaxyParser.so在mutex.o的左边,所以gcc认为没 有用到它,–as-needed将其忽略。但是前面说的动态库符号解析的特点导 致ld认为某些符号是加载的时候才去地址重定位的。但是 libGalaxyParser.so已经被忽略了。所以就算你写上了依赖的库,load的时 候也会找不到符号。但是为什么没有-Wl–as-needed的时候是正确的呢?没 有的话,ld会set NEEDED libGalaxyParser.so(用前面提到的查看动态库 依赖关系的办法可以验证)。load的时候还是可以找到符号的,所以正确。
正确的链接方式是:
g++ -shared -Wl,--as-needed PyGalaxy.o -lGalaxyParser -lc -lm -ldl -lpthread -L/home/ocaml/lib/ -lrt -o PyGalaxy.so
解决方案
在项目开发过层中尽量让lib是垂直关系,避免循环依赖;越是底层的库,越是往后面写!
例如:
g++ ... obj($?) -l(上层逻辑lib) -l(中间封装lib) -l(基础lib) -l(系统lib) -o $@
这样写可以避免很多问题,这个是在搭建项目的构建环境的过程中需要考虑清楚地,在编译和链接上浪费太多的生命不值得!
Linux gcc/g++链接编译顺序详解的更多相关文章
- gcc/g++等编译器 编译原理: 预处理,编译,汇编,链接各步骤详解
摘自http://blog.csdn.net/elfprincexu/article/details/45043971 gcc/g++等编译器 编译原理: 预处理,编译,汇编,链接各步骤详解 C和C+ ...
- gcc与g++的编译链接的示例详解
一.编译方式的示例详解 1. 编译C代码 代码如下:main.c /*! ************************************************************** ...
- GCC 概述:C 语言编译过程详解
Tags: C Description: 关于 GCC 的个人笔记 GCC 概述 对于 GCC 6.1 以及之后的版本,默认使用的 C++ 标准是 C++ 14:使用 -std=c++11 来指定使用 ...
- 折腾gcc/g++链接时.o文件及库的顺序问题(转)
转自: http://www.cnblogs.com/OCaml/archive/2012/06/18/2554086.html#sec-1-1 折腾gcc/g++链接时.o文件及库的顺序问题 Tab ...
- 折腾gcc/g++链接时.o文件及库的顺序问题
gcc/g++链接时.o文件以及库的顺序问题 1 写在前面 最近换了xubuntu12.4,把原来的项目co出来编译的时候报"undefined reference to".猜测是 ...
- gcc/g++链接时.o文件及库的顺序问题
折腾gcc/g++链接时.o文件及库的顺序问题 链接静态库的顺序问题 GCC 编译使用动态链接库和静态链接库--及先后顺序----及环境变量设置总结
- Linux中Nginx安装与配置详解
转载自:http://www.linuxidc.com/Linux/2016-08/134110.htm Linux中Nginx安装与配置详解(CentOS-6.5:nginx-1.5.0). 1 N ...
- Linux C 语言之 Hello World 详解
目录 Linux C 语言之 Hello World 详解 第一个 C 语言程序 程序运行原理 编译,链接 运行时 链接库 编译器优化 Hello World 打印原理 stdout, stdin 和 ...
- Oracle 11g客户端在Linux系统上的配置步骤详解
Oracle 11g客户端在Linux系统上的配置步骤详解 2011-07-26 10:47 newhappy2008 CSDN博客 字号:T | T 本文我们主要介绍了Oracle 11g客户端在L ...
随机推荐
- CentOS 7 安装java
我喜欢在centos中安装openjdk版本的java,无他,方便.虽然有一些不同之处,但不影响使用. 1.查询: yum search openjdk ,结果如下: java-1.6.0-openj ...
- 菜鸟教程之工具使用(二)——Maven打包非规范目录结构的Web项目
用过Maven的人都知道,Maven项目的目录结构跟传统的DynamicWeb项目有些不同.当然我们按照Maven的规范建项目最好,但是当你恰好没有按照Maven的规范来,又恰好需要使用Maven来打 ...
- Python 3.6 安装pip
1.首先python3.4以后,pip内嵌,不过使用方法是: python -m pip install django 2.如果你觉得上面的指令太长,想用旧方法使用pip,那用以下指令安装pip即可 ...
- linux命令(46):批量更改文件后缀,文件名
linux shell 1.要将所有 jpeg的后缀名图片文件修改为 jpg文件. rename .jpeg .jpg *.jpeg
- spark快速上手
spark快速上手 前言 基于Spark 2.1版本 仅仅是快速上手,没有深究细节 主要参考是官方文档 代码均为官方文档中代码,语言为Scala 进入spark-shell 终端输入spark-she ...
- SqlServer select * into 对应 Oracle语法
创建新表,并插入旧表值 Sql Server select * into new_emp from emp; Oracle create table new_emp as select * from ...
- 总结: Sort 排序算法
排序总结 面试经验 硅谷某前沿小Startup面试时,问到的一个题目就是写一个快速排序算法.进而面试官问到了各种算法的算法复杂度,进而又问了Merge Sort 与 QuickSort 的优劣. 对排 ...
- API:详解 pandas.read_csv
pandas.read_csv 作为常用的读取数据的常用API,使用频率非常高,但是API中可选的参数有哪些呢? pandas项目代码 答案是: .read_csv(filepath_or_buffe ...
- C语言版——点亮LED灯,深入到栈
在上一篇进行了汇编语言的编写之后,我们采用C语言来编写程序,毕竟C语言才是我们使用最多的语言. 仅仅是点亮LED灯显然太过于简单,我们需要分析最后的反汇编,了解函数调用栈,深入C语言骨髓去分析代码,并 ...
- C#学习笔记(26)——委托计算器
说明(2017-11-20 15:14:18): 1. 委托一直稀里糊涂的,有人说简单,有人说难,艹你们! 2. 其实主要是因为,平时不用委托也能解决大部分问题,所以都没有特意去用,网上也没有什么很好 ...