您的位置:首页 > 运维架构 > Linux

linux 下makefile的使用举例

2012-12-05 16:53 288 查看
该程序在linux平台下用C语言实现

有两个头文件m1.h m2.h 对应的实现文件m1.c m2.c 调试文件test.c

案例代码如下:

m1.h

#include<stdio.h>
void m1_print(char *str);


m2.h

#include<stdio.h>
void m2_print(char *str);


m1.c

#include<stdio.h>
#include"m1.h"
void m1_print(char *str){
printf("this is string %s\n",str);
}


m2.c

#include<stdio.h>
#include"m2.h"
void m2_print(char *str){
printf("this is string %s.",str);
}


test.c
#include<stdio.h>
#include"m1.h"
#include"m2.h"

void main(){
m1_print("Hello");
m2_print("World");
}


Makefile文件两种写法:

1.
main:test.o m1.o m2.o
	gcc -o testt test.o m1.o m2.o
test.o:test.c m1.h m2.h
	gcc -c test.c
m1.o:m1.c m1.h
	gcc -c m1.c
m2.o:m2.c m2.h
	gcc -c m2.c


2.
ma:test.o m1.o m2.o
	gcc -o $@ $^
test.o:test.c m1.h m2.h
	gcc -c $<
m1.o:m1.c m1.h
	gcc -c $<
m2.o:m2.c m2.h
	gcc -c $<


注意: 1. 三个常用变量:$@(目标文件),$^(所有依赖文件),$<(第一个依赖文件)

2.命令行处需要Tab键空行

3.makefile文件中编译命令其实就是一个递归过程

4. 编译指令make 或 make -f Makefile
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: