一个工程中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中,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. CSS3 文本效果(阴影)

    CSS3中包含几个新的文本特征. 在本章中您将了解以下文本属性: text-shadow box-shadow text-overflow word-wrap word-break CSS3 的文本阴 ...

  2. 转:js中javascript:void(0) 真正含义

    from:http://www.jb51.net/article/71532.htm 在Javascript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值. 我想使用过ajax的都常 ...

  3. UVALIVE 5096 Volume

    This time your job is to calculate the volume of a special object. The object consists of two orthog ...

  4. Buildroot自动化交叉编译工具:其一【转】

    转自:http://blog.csdn.net/youyudehexie/article/details/7583657 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] Bui ...

  5. Shell 脚本实现TCP/UDP协议通讯

    Shell 脚本实现TCP/UDP协议通讯 http://www.cnblogs.com/occult/archive/2012/12/25/2832183.html  

  6. 【LA3461】Leonardo的笔记本

    白书例题. #include<bits/stdc++.h> using namespace std; ],vis[],cnt[]; inline int read(){ ,x=;char ...

  7. springboot 404返回自定义json(只进入过滤器)

    今天在公司没事干,记一次springboot遇到的一些坑,在百度上也没有搜到类似的问题和答案(或者说 答案不是我想要的) 当我们在SpringBoot遇到了404或者500的错误的时候,你们会怎么办? ...

  8. golang的各种打印

    golang的打印方法太特么多了,下面就来区分一下之间的差异 package main import ( "fmt" "os" ) func main() { ...

  9. go语言的定时器

    package main import ( "fmt" "time" ) func main(){ //创建一个定时器,时间为2s,2s过后会自动往通道里面写入 ...

  10. 一点关于"fat model, skinny controller"的思考

    导言 想来从事服务器端开发也有将近一年的时间,服务端开发不能忽略的一个架构就是MVC架构,但一开始作为小白的我对这些高大上的概念也是很迷惑,由于很长一段时间应对的业务也是十分简单,业务代码也是流水一样 ...