Makefile编写

24 篇文章 0 订阅

1.  -c flag says to generate the object file

2. -o $@ says to put the output of the compilation in the file named on the left side of the : (冒号)

3. the $< is the first item in the dependencies list

4. let's use the special macros $@ and $^, which are the left and right sides of the :, respectively

5. makefile中具有特定含义的变量,如: CFLAGS, 可以参考[2]命名, 比较常用的有:

(1) CFLAGS, C编译器编译时需要的一些选项, eg. -g -O3, 以及编译时需要的头文件, eg. -I ./include. 此外CXXFLAGS是对应C++编译器下的选项, 但由于CFLAGS的广泛使用, CXXFLAGS用的比较少

(2) LDFLAGS, 告诉链接器从哪里寻找库文件[3], eg. -L/opt/mysql/lib

(3) LDLIBS, 告诉链接器要链接哪些库文件, eg.  -lmysqlclient

6. 一些编译选项

(1) -Wall, compiler warnings(含义: 打印出gcc提供的警告信息)

(2) -O0, -O1, -O2, -O3, 编译器的优化选项的4个级别,-O0表示没有优化,-O1为缺省值,-O3优化级别最高(不推荐使用, 特别是调试时)

(3) -Dmacro, 相当于C语言中的#define macro; -Dmacro=defn, 相当于C语言中的#define macro=defn

(4) -Wl.option, Pass options to linker. (此选项传递option给链接程序;如果option中间有逗号,就将option分成多个选项,然后传递给会链接程序.)

(5) -fPIC: Compiler directive to output position independent code, a characteristic required by shared libraries. Also see "-fpic".

(6) -shared: Produce a shared object which can then be linked with other objects to form an executable.

(7) -ldl, 指当需要"显式加载动态库的动态函数库"时才使用, 即使用到dlopen(),dlclose() , dlerror() , dlsym(), dlvsys()这些函数时, 它们都在头文件 #include <dlfcn.h>中.

7. (1) gcc - GNU C compiler

    (2) ld - The GNU Linker
    (3) ldd - List dependencies

8. 当一个静态库要使用另外一个静态库时:

假如有两个.a, libx.a 里面需要调用liby.a的接口函数

那么,将代码编译生成libx.a的时候,不需要链接liby.a的,只需要在编写.a源程序时#include"..."对应的头文件即可。

但是,当给调用者使用libx.a的时候,则必须同时链接两个库,且必须按-lx -ly的顺序(-ly -lx是不行的)


Reference

[1] A Simple Makefile Tutorial

[2] Variables Used by Implicit Rules

[3] makefile的选项CFLAGS、CPPFLAGS、LDFLAGS和LIBS的区别


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值