makefile for Linux纯小白版

某大佬曾说过:

不会makefile,不要说你会linux,因为makefile是合格程序员的必经之路

首先你要有个vi或者emacs 之类的编辑器

编写几个程序

test1.c

1 #include<stdio.h>
2 #include "test2.h"
3  
4 int main() 
5 {
6     printf("This is test1!\n");
7     PrintTest2();
8     return 0;
9 }

test2.c

1 #include<stdio.h>
2 #include "test2.h"
3  
4 int main() 
5 {
6     printf("This is test1!\n");
7     PrintTest2();
8     return 0;
9 }

test2.h:

1  #ifndef TEST2_H_
2  #define TEST2_H_
3  
4 void PrintTest2();
5 
6  #endif

然后你的makefile

需要建立一个文件叫makefile(可以是MAKEFILE)-------但是只能是这两个名字

接下来就是makefile中的内容

 1 test: test1.o test2.o //这是主要的文件,后面两个是形成的执行文件
 2     gcc -Wall test1.o test2.o -o test
 3     
 4 test1.o: test1.c test2.h//底下这些就是每一个执行文件所包含的.c  .h文件
 5     gcc -c -Wall test1.c -o test1.o//编译
 6 
 7 test2.o: test2.c test2.h//执行文件所包含的.c .h文件
 8     gcc -c -Wall test2.c -o test2.o
 9 
10 clean: //删除
11     rm -rf *.o test

运行的时候是

./test

可以用"代号":使用变量

前几行是声明

varname = xxx

底下写的时候就直接用就行

使用的时候要$(varname)

 1 OBJS = test1.o test2.o
 2 G = gcc
 3 CFLAGS = -Wall -O -g
 4  
 5 test:$(OBJS)
 6     $(G) $(OBJS) -o test
 7  
 8 test1.o:test1.c test2.h
 9     $(G) $(CFLAGS) -c test1.c
10 test2.o:test2.c test2.h
11     $(G) $(CFLAGS) -c test2.c
12  
13 clean:
14     rm -rf *.o test

makefile加油呀!

 

posted @ 2020-05-02 12:11  安之若醇  阅读(398)  评论(0编辑  收藏  举报
Live2D服务支持