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 ...
随机推荐
- MongoDB C Driver使用教程
MongoDB C Driver使用教程 转载请注明出处http://www.cnblogs.com/oloroso/ 本指南提供简介 MongoDB C 驱动程序. 在 C API 的详细信息,请参 ...
- Python递归及斐波那契数列
递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可 ...
- 【Eclipse】总结自己在工作中经常使用到的Eclipse快捷键
一些我觉得比较有用的快捷键,仅作参考. 1.alt + shift + c :更改方法签名. 2.三次鼠标左键单击: 选中一整行. 3.alt + shift + d/x: 再按t : 运行junit ...
- jstl 简单实践
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- oracle中根据当前记录查询前一条和后一条记录
select * from aa01_2014 where aaa001=(select c.p from (select aaa001,lag(aaa001,1,0) over (order by ...
- iOS //清除本地缓存
//清除本地缓存 -(void)clearCache{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT ...
- Maven搭建 Spring环境
http://www.cnblogs.com/huaizuo/p/4920308.html http://mvnrepository.com/artifact/commons-logging/comm ...
- Win10 Migrate apps to the Universal Windows Platform (UWP)
https://msdn.microsoft.com/en-us/library/mt148501.aspx
- MEF Parts Sample
namespace Microshaoft.MEF.Contracts { using System; public delegate void ExceptionEventHandler<TS ...
- div滚动底部加载li,window滚动底部加载li
DIV固定高度滚动条滚动最底部,加载新的li <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...