Libraries

Static Libraries

  • a collection of ordinary object files (目标文件的集合)
  • loaded at program link time (链接阶段加载)
  • 不再那么重要的优势
    • 节省编译时间
    • 理论上稍快的执行速度
  • use the -l option to specify the library
    • the -l option is a linker option, and thus needs to be placed AFTER the name of the file to be compiled

Shared Libraries

  • libraries that are loaded by programs when they start (程序启动时加载)
  • soname - real name - linker name
    • a symbolic link to the shared library's "real name''
Thus, /usr/lib/libreadline.so.3 is a fully-qualified soname, which ldconfig would set to be a symbolic link to some realname like /usr/lib/libreadline.so.3.0. There should also be a linker name, /usr/lib/libreadline.so which could be a symbolic link referring to /usr/lib/libreadline.so.3.

Dynamically Loaded (DL) Libraries

  • libraries that are loaded at times other than during the startup of a program (程序启动之后才加载的)

    • they permit waiting to load the plugin until it's needed
  • built as standard object files or standard shared libraries
    • there is an API for opening a library, looking up symbols, handling errors, and closing the library

makefile中一些编译器选项

CFLAGS CXXFLAGS

  • CFLAGS 表示用于 C 编译器的选项
  • CXXFLAGS 表示用于 C++ 编译器的选项
  • 这两个变量实际上涵盖了 编译汇编 两个步骤
  • 指定头文件(.h文件)的路径
    • CFLAGS=-I/usr/include -I/path/include

(安装一个包时会在安装路径下建立一个include目录,当安装过程中出现问题时,试着把以前安装的包的include目录加入到该变量中来)

LDFLAGS

  • gcc等编译器会用到的一些优化参数
  • 指定 库文件的 位置 ( 静态库文件 连接阶段使用)
  • LDFLAGS=-L/usr/lib -L/path/to/your/lib

(每安装一个包都几乎一定的会在安装目录里建立一个lib目录。如果明明安装了某个包,而安装另一个包时出现找不到已经安装的包时,可以把那个包的lib路径加入的LDFALGS中试一下)

LIBS

  • 告诉链接器要链接哪些库文件
  • LIBS = -lpthread -liconv

(简单地说,LDFLAGS/-L是告诉链接器从哪里寻找库文件,而LIBS/-l是告诉链接器要链接哪些库文件。不过使用时 链接阶段 这两个参数都会加上,所以你即使将这两个的值互换,也没有问题)

运行时的链接操作

1

有时候LDFLAGS指定-L虽然能让链接器找到库进行链接,但是运行时链接器却找不到这个库,如果要让软件运行时库文件的路径也得到扩展,那么我们需要增加这两个库给"-Wl,R":

LDFLAGS = -L/var/xxx/lib -L/opt/mysql/lib -Wl,R/var/xxx/lib -Wl,R/opt/mysql/lib

如果在执行./configure以前设置环境变量export LDFLAGS="-L/var/xxx/lib -L/opt/mysql/lib -Wl,R/var/xxx/lib -Wl,R/opt/mysql/lib" ,注意 设置环境变量等号两边不可以有空格,而且要加上 引号 (shell的用法)。

那么执行configure以后,Makefile将会设置这个选项,链接时会有这个参数,编译出来的可执行程序的库文件搜索路径就得到扩展了。

2 -wl,-rpath=<link_path>

  • -Wl,-rpath,/usr/local/thrift0.11.0/lib:/usr/local/lib_boost_1_68_0:/usr/local/lib:/usr/local/lib64
  • 使用场景
    • A库依赖B库,只链接了A库时
    • 指定程序运行过程中查找库的路径
Add a directory to the runtime library search path. This is used when linking an ELF executable with shared objects. All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at runtime. The -rpath option is also used when locating shared objects which are needed by shared objects explicitly included in the link;

LD_LIBRARY_PATH环境变量

  • 指定 的共享库/动态库 搜索路径 (运行阶段使用)
  • echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/usr/local/lib64/:/usr/local/lib/:/usr/lib64/:/usr/lib/:/lib/:/lib64/ :/usr/local/cuda-8.0/lib64/" >> /home/worker/.bashrc
In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories;
  • LD_LIBRARY_PATH环境变量 指定的是 链接 时查找库的顺序 ,还是 运行 时查找库的顺序?

  • 配置文件/etc/ld.so.conf 与 ldconf

    • 待补充

头文件、库文件的搜索顺序

头文件

静态库

动态库

参考Linux系统 GCC搜索头文件和库文件的执行顺序

参考资料

makefile中一些编译器选项的更多相关文章

  1. Makefile中的特殊宏定义以及实用选项

    Makefile中的一些特殊宏定义的名字跟shell中的位置变量挺相似的. $?    当前目标所依赖的文件列表中比当前目标文件还要新的文件 $@   当前目标我名字 $<   当前依赖文件的名 ...

  2. makefile中的自动化变量$@,$%,$

    转自:http://www.2cto.com/os/201302/191344.html   makefile中的自动化变量$@,$%,$   自动化变量  模式规则中,规则的目标和依赖文件名代表了一 ...

  3. makefile 中 $@ $^ %< 使用【转】

    转自:http://blog.csdn.net/kesaihao862/article/details/7332528 这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将 ...

  4. makefile 中 $@ $^ %< 使用

    这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将会学到以下内容: 源程序编译 Makefile的编写 程序库的链接 程序的调试 头文件和系统求助 1.源程序的编译 在L ...

  5. Makefile中用宏定义进行条件编译(gcc -D)/在Makefile中进行宏定义-D【转】

    本文转载自:http://blog.csdn.net/maopig/article/details/7230311 在源代码里面如果这样是定义的:#ifdef   MACRONAME//可选代码#en ...

  6. Makefile中自动生成头文件依赖

    为什么需要自动生成头文件依赖? 编译单个源文件时,需要获取文件中包含的头文件的信息,但是一般的Makefile不会在规则中明确写明文件依赖的头文件,所以单独修改头文件后,不会导致包含头文件的源文件重新 ...

  7. OJ提交题目中的语言选项里G++与C++的区别(转)

    G++? 首先更正一个概念,C++是一门计算机编程语言,G++不是语言,是一款编译器中编译C++程序的命令而已. 那么他们之间的区别是什么? 在提交题目中的语言选项里,G++和C++都代表编译的方式. ...

  8. Makefile中指示符“include”、“-include”和“sinclude”的区别

    转:http://www.cnblogs.com/xmphoenix/archive/2012/02/22/2363335.html 指示符“include”.“-include”和“sinclude ...

  9. makefile中使用变量

    makefile里的变量就像一个变量,变量的作用主要如下: (1)保存文件名列表. (2)保存编译器的参数. makefile中的变量是用一个字符串在makefile中定义的,这个文本串就是变量的值. ...

随机推荐

  1. nodeppt:网页版 PPT

    资料 网址 github https://github.com/ksky521/nodeppt 网页版PPT(nodeppt 的介绍) http://deliazhi.com/2017/03/31/W ...

  2. 1.2.1 Excel如何从身份证号中提取日期

    在对应的单元格中我们输入公式: =MID(B3,7,4)&"年"&MID(B3,11,2)&"月"&MID(B3,13,2)&a ...

  3. [转]使用python爬取东方财富网机构调研数据

    最近有一个需求,需要爬取东方财富网的机构调研数据.数据所在的网页地址为: 机构调研 网页如下所示: 可见数据共有8464页,此处不能直接使用scrapy爬虫进行爬取,因为点击下一页时,浏览器只是发起了 ...

  4. 图像小波变换去噪——MATLAB实现

    clear; [A,map]=imread('C:\Users\wangd\Documents\MATLAB\1.jpg'); X=rgb2gray(A); %画出原始图像 subplot(,,);i ...

  5. 02-模拟Junit4功能

    package com.day2; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; im ...

  6. vm虚拟机 模板机进行克隆导致centos 7.2 无法加载网卡

    问题描述:vm虚拟机 模板机进行克隆导致centos 7.2 无法加载网卡. 1.ifconfig 查看网卡状态 lo: flags=<UP,LOOPBACK,RUNNING> mtu i ...

  7. pyton unittest

    在说unittest之前,先说几个概念: TestCase 也就是测试用例 TestSuite 多个测试用例集合在一起,就是TestSuite TestLoader是用来加载TestCase到Test ...

  8. how find out what is causing Visual Studio to think each project is out of date

    You can find out what is causing Visual Studio to think each project is out of date, and then addres ...

  9. Laravel常见问题集锦

    1.提示:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes? ...

  10. ATS配置自定义日志

    修改records.config,开启日志自定义功能 更改日志目录,默认日志存放在/var/log/trafficserver: CONFIG proxy.config.log.logfile_dir ...