gcc编译命令行依赖库的指定顺序
gcc链接过程中定义了三个集合:可重定位目标文件集合E、未解析符号集合U和已定义符号集合D,链接基本流程如下:
1) 按命令行指定顺序依次处理每个目标文件和库文件;
2) 如果为目标文件,将其加入集合E,将其所有符号加入D,将得到解析的符号从U中移除;
3) 如果为静态库文件,若库中的某个目标文件能够解析U中的一个或多个符号,则按1)中方式进行链接处理;
4) 如果为动态库文件,移除U中得到解析的符号,在D中加入库中符号;
5) 若集合U最终为空,链接成功,对E中文件进行合并和重定位处理,生成可执行文件,否则报告链接出错(输出“undefined reference to......”)。
根据gcc链接器工作原理,被依赖目标文件(或库)必须放在引用该文件的目标文件(或库)的后面(如果放在前面,链接器在处理被依赖文件时,不知道后面会有对该文件的引用,被引用的符号就不会提取出来),gcc手册中对这种依赖的描述如下:
-l library
native 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.
如果确实无法理清依赖顺序,可以采用如下两种方法:
a) 重复指定,如gcc -o test.c -la -lb -la;
b) 使用Xlinker选项,如gcc -o test.c -Xlinker "-(" -la -lb "-)",gcc手册中对Xlinker的描述如下:
-Xlinker option
Pass option as an option to the linker. You can use this to
supply system-specific linker options that GCC does not
recognize. If you want to pass an option that takes a separate argument, you
must use -Xlinker twice, once for the option and once for the
argument. For example, to pass -assert definitions, you must
write -Xlinker -assert -Xlinker definitions. It does not work to
write -Xlinker "-assert definitions", because this passes the
entire string as a single argument, which is not what the linker
expects. When using the GNU linker, it is usually more convenient to pass
arguments to linker options using the option=value syntax than as
separate arguments. For example, you can specify -Xlinker
-Map=output.map rather than -Xlinker -Map -Xlinker output.map.
Other linkers may not support this syntax for command-line
options.
gcc编译命令行依赖库的指定顺序的更多相关文章
- GCC 命令行详解 -L 指定库的路径 -l 指定需连接的库名(转载)
转载自:http://www.cnblogs.com/cy163/archive/2009/03/12/1409434.html 1.gcc包含的c/c++编译器gcc,cc,c++,g++,gcc和 ...
- GCC 命令行详解 -L 指定库的路径 -l 指定需连接的库名 zhuan
1.gcc包含的c/c++编译器gcc,cc,c++,g++,gcc和cc是一样的,c++和g++是一样的,(没有看太明白前面这半句是什么意思:))一般c程序就用gcc编译,c++程序就用g++编译 ...
- 在Linux使用GCC编译C语言共享库
在Linux使用GCC编译C语言共享库 对任何程序员来说库都是必不可少的.所谓的库是指已经编译好的供你使用的代码.它们常常提供一些通用功能,例如链表和二叉树可以用来保存任何数据,或者是一个特定的功能例 ...
- GCC常用命令行一览表
GCC常用命令行一览表 这些常用的 gcc/g++ 命令行参数,你都知道么?1. gcc -E source_file.c-E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_ ...
- GCC编译命令常用选项
GCC是GUN Compiler Collection的简称,除编译程序外,还包含其他相关工具.GCC可将高级语言编写的源代码构建成计算机直接执行的二进制代码.GCC是Linux平台下最常用的编译程序 ...
- GCC 编译命令
今天突然被同事问道一个GCC编译命令为的问题,感觉对相应内容生疏了,赶紧整理下相关内容,梳理下相关知识. GCC命令提供了非常多的命令选项,但并不是所有都要熟悉,初学时掌握几个常用的就可以了,到后面再 ...
- 【C++】cmdline——轻量级的C++命令行解析库
1.说明 cmdline是一个轻量级的c++命令行参数解析工具,全部源码只有一个cmdline.h头文件. 2.代码 20171210_命令行进行解析.cpp // 20171210_命令行进行解析. ...
- 微软官方的.net命令行参数库
虽然现在是图形化大行其道的时代,但让程序支持命令行启动对于专业的领域还是有不少需求的..net本身并没有内置对命令行解析的支持,我之前就写过一篇文章让.Net程序支持命令行启动介绍了几个第三方的命令行 ...
- Visual Studio命令行创建库文件lib
Visual Studio命令行创建库文件lib OS:win7 旗舰版SP1 64位 编译器: VS 2013 express 的cl 建一个文件Static_Lib.h,源代码如下 #ifndef ...
随机推荐
- difference bewteen *args and **args in python?
It's also worth noting that you can use * and ** when calling functions as well. This is a shortcut ...
- linux下nat配置
iptables要启用nat表,必须启动nat表的支持.默认情况下,linux下是没有开启nat表的支持的. #启动内核的路由功能 echo > /proc/sys/net/ipv4/ip_fo ...
- MVC4.0
--后台ajax传值 <script type="text/javascript"> $(document).ready(function dianZan(id ...
- ASP.NET MVC随想录——锋利的KATANA
正如上篇文章所述那样,OWIN在Web Server与Web Application之间定义了一套规范(Specs),意在解耦Web Server与Web Application,从而推进跨平台的实现 ...
- Modbus总线CRC16效验算法C语言
unsigned short CRC16_Modbus ( unsigned char *pdata, int len){ unsigned short crc=0xFFFF; int i, j; f ...
- jquery 键盘回车事件
<input id="search" placeholder="输入要领用的资产条码" id="scanCode" type=&quo ...
- HDU5880 Family View(2016青岛网络赛 AC自动机)
题意:将匹配的串用'*'代替 tips: 1 注意内存的使用,据说g++中指针占8字节,c++4字节,所以用g++交会MLE 2 注意这种例子, 12abcdbcabc 故失败指针要一直往下走,否则会 ...
- 铁区MES部分页面展示
激活码: 76231722-2e7554593-b750-07e2f4844531 TIP: 若您激活不成功,可能是因为您所安装的软件版本较低,请尝试以下激活码 激活码: RXWY-A25421-K5 ...
- 第六届福建省大学生程序设计竞赛(FZU2213—FZU2221)
from:piaocoder Common Tangents(两圆之间的公公切线) 题目链接: http://acm.fzu.edu.cn/problem.php?pid=2213 解题思路: 告诉你 ...
- 水晶报表初体验(Visual Studio 2010)
安装水晶报表后如下使用: 配置rpt文件,如图 前台(Asp.net页面): <%@ Register Assembly="CrystalDecisions.Web, Version= ...