makefile中一些编译器选项

时间:2021-11-20 20:40:04

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搜索头文件和库文件的执行顺序

参考资料

相关文章