Makefile的制作
一个工程中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中,makefile定义系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为makefile就想一个Shell脚本一样,其中也可以执操作系统的命令。
Linux环境下的程序员如果不会使用GUN make来构建和管理自己的工程,应该不能算是一个合格的专业程序员。在Linux(Unix)环境下使用GUN的make工具能够比较容易的构建一个属于你自己的工程,整个工程的编译只需要一个命令就可以完成编译、连接以至于最后的执行。不过这需要我们投入一些时间去完成一个或者多个称之为makefile文件的编写。
所要完成的makefile文件描述了整个工程的编译、连接等规则。其中包括:工程的哪些源文件需要编译、需要创建哪些库文件以及如何创建这些库文件、如何最后产生我们想要的可执行文件。尽管看起来可能是很复杂的事情,但是为了工程编写makefile的好处是能够使用一行命令来完成“自动化编译”,一旦提供一个正确的makefile。编译整个工程你嗦要做的唯一的一件事就是在shell提示符下输入make命令,这个工程完全自动编译,极大地提高了效率。
Makefile结构
#表示注释
变量定义
VAR=test 定义变量VAR,强制付志伟test
VAR+=app 在VAR之前定义的值后面再追加app这个值
VAR?=testapp 如果之前VAR没有被定义,则定义并使用testapp,否则使用之前的值
第一条目标为总的目标
依赖可以是文件(目录)或为其他目标
动作可以是Linux命令,动作的哪一行第一个字符必须是以TAB键补齐的
target: depend1 depend2 depend3 ......
[TAB] action1
[TAB] action2
target:
[TAB] action1
[TAB] action2
makefile的使用
make找makefile或者Makefile文件执行总的目标
make clean 执行makefile文件中的clean目标
make -C directory 进入到directory文件夹中去执行总的目标
make clean -c directory 进入到directory文件夹中去执行clean目标
make -f comm_makefile 通过-f选项指定一个makefile文件
make VAR=value 给makefile传一个参数VAR,其值为value
案例:
分别创建vendor1与vendor2两个文件夹,在这两个文件夹中放两个函数
[xiaohexiansheng@centos6 library]$ mkdir vendor1
[xiaohexiansheng@centos6 library]$ mkdir vendor2
[xiaohexiansheng@centos6 library]$ cd vendor1
[xiaohexiansheng@centos6 vendor1]$ vim crypto.c
#include <stdio.h> void crypto(void)
{
printf("Start crypt..."); return ;
}
[xiaohexiansheng@centos6 vendor1]$ vim crypto.h
#ifndef _CRYPTO_H_
#define _CRYPTO_H_ void crypto(void); #endif
[xiaohexiansheng@centos6 vendor1]$ vim makefile
LIB_NAME=vendor1 all: shared static
rm -f *.o shared:
gcc -shared -fpic -o lib${LIB_NAME}.so *.c static:
gcc -c *.c
ar -rcs lib${LIB_NAME}.a *.o clean:
rm -f *.a *.so
rm -f *.o install:
cp lib${LIB_NAME}.* ../libs
[xiaohexiansheng@centos6 vendor1]$ cd ../vendor2
[xiaohexiansheng@centos6 vendor2]$ vim func.c
#include <stdio.h> void func(void)
{
printf("Start func..."); return ;
}
[xiaohexiansheng@centos6 vendor2]$ vim func.h
#ifndef _FUNC_H_
#define _FUNC_H_ void func(void); #endif
[xiaohexiansheng@centos6 vendor2]$ vim makefile
LIB_NAME=vendor2 all: shared static
rm -f *.o shared:
gcc -shared -fpic -o lib${LIB_NAME}.so *.c static:
gcc -c *.c
ar -rcs lib${LIB_NAME}.a *.o clean:
rm -f *.a *.so
rm -f *.o install:
cp lib${LIB_NAME}.* ../libs
[xiaohexiansheng@centos6 vendor2]$ cd ..
[xiaohexiansheng@centos6 library]$ vim main.c
#include "crypto.h"
#include "func.h" int main(void)
{
crypto();
func(); return ;
}
[xiaohexiansheng@centos6 library]$ vim makefile
APP_NAME?=app all: lib_vendor1 lib_vendor2
gcc -static main.c -Ivendor1 -Ivendor2 -o ${APP_NAME} -Lvendor1 -Lvendor2 -lvendor1 -lvendor2 lib_vendor1:
make -C vendor1 lib_vendor2:
make -C vendor2
[xiaohexiansheng@centos6 library]$ ls
main.c makefile vendor1 vendor2
[xiaohexiansheng@centos6 library]$ make
make -C vendor1
make[1]: Entering directory `/home/xiaohexiansheng/cc/library/vendor1'
gcc -shared -fpic -o libvendor1.so *.c
gcc -c *.c
ar -rcs libvendor1.a *.o
rm -f *.o
make[1]: Leaving directory `/home/xiaohexiansheng/cc/library/vendor1'
make -C vendor2
make[1]: Entering directory `/home/xiaohexiansheng/cc/library/vendor2'
gcc -shared -fpic -o libvendor2.so *.c
gcc -c *.c
ar -rcs libvendor2.a *.o
rm -f *.o
make[1]: Leaving directory `/home/xiaohexiansheng/cc/library/vendor2'
gcc main.c -Ivendor1 -Ivendor2 -o app -Lvendor1 -Lvendor2 -lvendor1 -lvendor2
[xiaohexiansheng@centos6 library]$ ls
app main.c makefile vendor1 vendor2
Makefile的制作的更多相关文章
- makefile文件制作入门
一.首先,看一下最简单的C文件 //hello.c文件 #include <stdio.h> void main() { printf("hello world\n") ...
- C语言Makefile文件制作
本文摘抄自“跟我一起写Makefile ”,只是原文中我自己感觉比较精要的一部分,并且只针对C语言,使用GCC编译器. 原文请看这里:http://wiki.ubuntu.org.cn/%E8%B7% ...
- 第三课 Makefile文件的制作(上)
1.序言: 前面的课程讲解了从gcc编译过程到其实践,大家可以看到其实在这些步骤中有些是可以简化编译的,但由于参数多以及项目中文件数量多的原因难免会造成错误甚至是浪费大量的时间在这编译上,为此linu ...
- Makefile学习笔记
ls -l 查看文件详细信息 1.gcc -E test.c -o test.i//预编译gedit test.i //查看:高级C 2.gcc -Wall -S test.i -o test.s// ...
- kernel Makefile Kconfig说明
实际文档位置:Documentation/kbuild/makefiles.txt,此为翻译稿. *************************************************** ...
- makefile编写helloworld
相信在unix下编程的没有不知道makefile的,刚开始学习unix平台 下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其简单的例子: 现在我要编译一个Hello ...
- 通过busybox制作根文件系统
通过busybox制作根文件系统可以自定义选项,在制作的根文件系统中添加需要的命令,指定生成的根文件系统到相应的目录下. 一. 根文件系统的获取方式--->官网: https://busybox ...
- 简单makefile示例
Makefile cmd: - g++ 相信在linux下编程的没有不知道makefile的,刚开始学习linux平台下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其 ...
- u-boot移植总结(四)u-boot-2010.09框架分析
(一)本次移植是基于FL2440,板子的基本硬件: CPU 型号为S3C2440,基于ARM920T,指令集ARMV4,时钟主频400MHz SDRAM H57V2562GTR-75C 2片*32MB ...
随机推荐
- unicode字符串解码显示
# encoding: utf-8 ''' unicode字符串解码显示 ''' import sys reload(sys) sys.setdefaultencoding('utf-8') a = ...
- stylus安装以及使用命令行生成css文件
Stylus 是一个CSS的预处理框架,2010年产生,来自Node.js社区,主要用来给Node项目进行CSS预处理支持,所以 Stylus 是一种新型语言,可以创建健壮的.动态的.富有表现力的CS ...
- OpenCV——Mat、CvMat、IplImage类型浅析【转】
OpenCV中常见的与图像操作有关的数据容器有Mat,cvMat和IplImage. 一.Mat类型:矩阵类型,Matrix. 在openCV中,Mat是一个多维的密集数据数组.可以用来处理向量和矩阵 ...
- vue学习记录:vue引入,validator验证,数据信息,vuex数据共享
最近在学习vue,关于学习过程中所遇到的问题进行记录,包含vue引入,validator验证,数据信息,vuex数据共享,传值问题记录 1.vue 引入vue vue的大致形式如下: <temp ...
- python 垃圾回收详解
原文:https://zhuanlan.zhihu.com/p/31150408 总纲 策略和垃圾回收系统工作内容 引用计数详解 标记-清除+分代收集 循环引用 编程应用-常见方法 ex 过程详解 使 ...
- Android 开发实用方法大全
1.格式化价格,这个经常在计算费用精度的时候用到 /** * 格式化价格 * * @param argStr 传入价格字符串 * @return */ public static String get ...
- 如何理解reliability
首先推荐看: https://wenku.baidu.com/view/f55f400c52ea551810a68746.html 复习一下均值方差 然后重点看: https://www.social ...
- [bootstrap] 修改字体
file: variable.less @font-family-sans-serif: "Helvetica Neue", Helvetica, Arial, sans-seri ...
- [thinkPHP] buildSql可以查看tp CURD操作对应的SQL
$goods = M('Goods')->where($map)->buildSql(); echo $goods;
- $_ENV 为空的原因
php.ini里面的variables_order的值为GPCS,修改为EGPCS,然后重启wamp即可. print_r($_ENV); var_dump($_ENV); foreach ($_EN ...