在程序中用到某个静态库,使用命令:

gcc bin -llibrary.a object.o 

结果发现找不到library.a中的某些函数符号

undefine reference to ...

通过nm命令查看library.a,发现该函数符号是存在的,说明library.a本身是没有问题的,百思不得其解,于是胡乱猜测,猜测可能是命令选项的顺序有问题,于是调整命令

gcc bin object.o -llibrary.a

编译成功,但是为什么呢?难道-l选项还有位置要求?于是通过man ld查看,-l选项描述得很少,但并没有提到-l位置的问题。于是百度得知:

http://hi.baidu.com/xiexin/item/2168cf28aca6d8ceddf69a47 (UNIX程序的链接)

链接程序对于静态库的搜索仅仅是为了解决以前发现的、尚未定义的外部引用。因此,-l选项在命令行中的位置很重要。例如:

$ cc -dn file1.c -lm file2.c

这样链接程序对libm.a的搜索只是为了解决file1.c中对数学函数的调用。因此如果在file2.c中调用了某个数学函数,链接程序将无法找到该函数的定义,因而链接也将失败。这种情况下,

除非是特别清楚每个C文件中都调用了哪些函数,否则还是将-l选项放在命令行的最后比较好,并且-l选项可以出现多次,以指定多个不同的库文件。

找到问题所在,这么重要的细节,gcc不可能不在文档里进行描述啊,于是再次man ld, 仔细看了一下,发现

This man page does not describe the command language; see the ld entry in "info" for full details on the command language and on other aspects of the GNU linker.

于是调用 info ld 命令,终于看到-l选项详尽的描述:

-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of
files to link. This option may be used any number of times. If
namespec is of the form :filename, ld will search the library path
for a file called filename, otherwise it will search the library
path for a file called libnamespec.a. On systems which support shared libraries, ld may also search for
files other than libnamespec.a. Specifically, on ELF and SunOS
systems, ld will search a directory for a library called
libnamespec.so before searching for one called libnamespec.a. (By
convention, a ".so" extension indicates a shared library.) Note
that this behavior does not apply to :filename, which always
specifies a file called filename. The linker will search an archive only once, at the location where
it is specified on the command line. If the archive defines a
symbol which was undefined in some object which appeared before the
archive on the command line, the linker will include the
appropriate file(s) from the archive. However, an undefined symbol
in an object appearing later on the command line will not cause the
linker to search the archive again. See the -( option for a way to force the linker to search archives
multiple times. You may list the same archive multiple times on the command line. This type of archive searching is standard for Unix linkers.
However, if you are using ld on AIX, note that it is different from
the behaviour of the AIX linker.

通过man gcc也能看到-l的相关描述

ld -l选项注意事项的更多相关文章

  1. GCC -l选项:手动添加链接库

    链接器把多个二进制的目标文件(object file)链接成一个单独的可执行文件.在链接过程中,它必须把符号(变量名.函数名等一些列标识符)用对应的数据的内存地址(变量地址.函数地址等)替代,以完成程 ...

  2. ld 链接选项-L,-rpath-link,-rpath

    转载自:http://blog.csdn.net/q1302182594/article/details/42102961 1. 三个C文件 1. world.c #include<stdio. ...

  3. GCC中 -I、-L、-l 选项的作用

    在makefile中经常会看到这些选项,gcc默认会在程序当前目录.path路径中查找所需要的材料 如何给gcc添加我们自己的原材料(头文件,库等) -I (注意是大写的i) 给gcc添加自定义的头文 ...

  4. python selenium 定制启动Chrome的选项注意事项(十九)

    自动化测试的时候为了避免每个case均需要登录的操作,所以把登录后的cookie信息保存下来,在有效期内使用cookie的方式实现登录操作,为了避免展现太多的登录操作,需要通过设置浏览器的option ...

  5. ansible-playbook -l 选项

    -l <SUBSET>, --limit <SUBSET> further limit selected hosts to an additional pattern 限制脚本 ...

  6. Linux ToolChain (二) --- Linker (1)链接选项 -L -rpath -rpath-link

    一.动态库的链接和链接选项-L,-rpath-link,-rpath (1). 现代连接器在处理动态库时将链接时路径(Link-time path)和运行时路径(Run-time path)分开, 用 ...

  7. 动态库的链接和链接选项-L,-rpath-link,-rpath

    https://my.oschina.net/shelllife/blog/115958 链接动态库 如何程序在连接时使用了共享库,就必须在运行的时候能够找到共享库的位置.linux的可执行程序在执行 ...

  8. Linux下ld搜索问题:ld: cannot find -l"XX"

    ld命令行工具(链接库的一个工具)的搜索路径是-L指定的,库名是-l指定的. 比如: ld -L[dir] -l[mylib] --verbose 以上我用可视化的方法显示ld的搜索路径,其结果是居然 ...

  9. gcc ld 链接器相关知识,调试指令(程序员的自我修养----链接、装载与库)

    最近解决一个动态链接上的问题,因为以前从来没有接触过这方面的知识,所以恶补了一下,首先要了解gcc编译指令(makefile),ld链接器的选项(还有连接脚本section指定内存位置),熟悉查看连接 ...

随机推荐

  1. Java Excel 插入图片

    在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片.所以要在Excel中 ...

  2. MVC工作原理

    MVC(Model-View-Controller,模型—视图—控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Controller) ...

  3. ESP8266学习笔记6:ESP8266规范wifi连接操作

    一.前言 我整理了从2015年至今关于ESP8266的学习笔记,梳理出来了开发环境.基础功能.进阶学习三大部分.方便自己和他人.可点此查看,欢迎交流. 之前在笔记4<ESP8266的SmartC ...

  4. EffectiveJava(26)使用泛型类替代普通类

    使用泛型编写类比使用需要在客户端代码中进行转换的类型更加安全,并且对去其他程序员来说更加容易扩展,我们应该将可以用泛型代替的非泛型类优化 那么,如何将类泛型化呢? 这很简单.首先,给他的声明添加一个或 ...

  5. 云计算之路-试用Azure:搭建自己的内网DNS服务器

    之前我们写过一篇博文谈到Azure内置的内网DNS服务器不能跨Cloud Service,而我们的虚拟机部署场景恰恰需要跨多个Cloud Service,所以目前只能选择用Azure虚拟机搭建自己的内 ...

  6. 建站笔记1:centos6.5下安装mysql

    近期买了个域名,想要玩玩自己建站点:接下来遇到的问题都会一次记录下来.以备自己以后复习查看: 首先建站方案选择: wordPress +centos6.5 +mysql; server买的:搬瓦工最低 ...

  7. Windows最强ssh客户端推荐 —— Bitvise SSH Client

    原名Tunnelier,解除它是因为为了sshFQ,没想到它既有SSH Terminal,又集成SFTP,还能FQ,功能一应区全. 还支持pem证书,回想最初使用putty,还要把pem证书进行转换才 ...

  8. 《暗黑世界V1.3》数据库表说明文档

    <暗黑世界V1.3>数据库表说明文档 (下载地址:http://www.9miao.com/forum.php?mod=viewthread&tid=38821&highl ...

  9. ES6 iterator 迭代器

    iterator使用TypeScript 的描述: interface Iterable { [Symbol.iterator]() : Iterator, } interface Iterator ...

  10. 【Linux】VMware上安装Linux操作系统

    Vmware上安装Linux系统 1. 文件菜单选择新建虚拟机 2. 选择经典类型安装,下一步. 3. 选择稍后安装操作系统,下一步. 4. 选择Linux系统,版本选择CentOS 64位. 给虚拟 ...