一个工程中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中,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的制作的更多相关文章

  1. makefile文件制作入门

    一.首先,看一下最简单的C文件 //hello.c文件 #include <stdio.h> void main() { printf("hello world\n") ...

  2. C语言Makefile文件制作

    本文摘抄自“跟我一起写Makefile ”,只是原文中我自己感觉比较精要的一部分,并且只针对C语言,使用GCC编译器. 原文请看这里:http://wiki.ubuntu.org.cn/%E8%B7% ...

  3. 第三课 Makefile文件的制作(上)

    1.序言: 前面的课程讲解了从gcc编译过程到其实践,大家可以看到其实在这些步骤中有些是可以简化编译的,但由于参数多以及项目中文件数量多的原因难免会造成错误甚至是浪费大量的时间在这编译上,为此linu ...

  4. Makefile学习笔记

    ls -l 查看文件详细信息 1.gcc -E test.c -o test.i//预编译gedit test.i //查看:高级C 2.gcc -Wall -S test.i -o test.s// ...

  5. kernel Makefile Kconfig说明

    实际文档位置:Documentation/kbuild/makefiles.txt,此为翻译稿. *************************************************** ...

  6. makefile编写helloworld

    相信在unix下编程的没有不知道makefile的,刚开始学习unix平台 下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其简单的例子: 现在我要编译一个Hello ...

  7. 通过busybox制作根文件系统

    通过busybox制作根文件系统可以自定义选项,在制作的根文件系统中添加需要的命令,指定生成的根文件系统到相应的目录下. 一. 根文件系统的获取方式--->官网: https://busybox ...

  8. 简单makefile示例

    Makefile cmd: - g++ 相信在linux下编程的没有不知道makefile的,刚开始学习linux平台下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其 ...

  9. u-boot移植总结(四)u-boot-2010.09框架分析

    (一)本次移植是基于FL2440,板子的基本硬件: CPU 型号为S3C2440,基于ARM920T,指令集ARMV4,时钟主频400MHz SDRAM H57V2562GTR-75C 2片*32MB ...

随机推荐

  1. 用树莓派做FTP服务器

    我为了传输文件方便,所以先简单的做了一个匿名ftp服务器 首先要下载ftp服务器软件 输入 sudo apt-get install vsftpd 安装vsftp 然后编辑 /etc/vsftp.co ...

  2. Django-models,继承AbstractUser类

    1.UserInfo类继承了Django模型自带的User类,需要导入AbstractUser   2.然后在settings.py中配置

  3. UVA 10471 Gift Exchanging

    题意:就5种盒子,给出每个盒子个数,盒子总数,每个人选择这个盒子的概率.求这个人选择哪个盒子取得第一个朋友的概率最大,最大多少 dp[N][sta]表示当前第N个人面临状态sta(选择盒子的状态可以用 ...

  4. [ Python - 15 ] win7安装paramiko问题总汇

    安装环境: win7 sp1 python3.5 安装paramiko 新装的win7 sp1 x64位系统,安装好python3.5和pycharm后,需要用到paramiko模块于是开始安装: & ...

  5. Centos下yum update与yum upgrade的区别

    转载来源于:http://www.cnblogs.com/EasonJim/p/9026357.html 说明:生产环境对软件版本和内核版本要求非常精确,别没事有事随便的进行yum update操作! ...

  6. hadoop3.1伪分布式部署

    1.环境准备 系统版本:CentOS7.5 主机名:node01 hadoop3.1 的下载地址: http://mirror.bit.edu.cn/apache/hadoop/common/hado ...

  7. java网络通信:TCP协议

    面试的时候,面试官由于需要考察一个面试人对于网络编程的熟悉程度,往往会考察学生对于TCP.HTTP.UDP.这些常见的网络编程当中的协议的了解程度,而TCP协议则是首当其冲的,作为进程之间通信常用的一 ...

  8. npm命令要记

    npm list - depth 0 查看依赖 cnpm install 安装 npm outdated 查看模块过时 npm cache clear

  9. HDU 6273.Master of GCD-差分数组 (2017中国大学生程序设计竞赛-杭州站-重现赛(感谢浙江理工))

    Super-palindrome 题面地址:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf 这道题是差分数组的题目,线 ...

  10. CF 917A The Monster 【括号匹配】

    [链接]:CF Examples inputCopy ((?)) outputCopy 4 inputCopy ??()?? outputCopy 7 说明 For the first sample ...