makefile

Linux 配置

c 编译器:gcc(属于GUN项目)
文本编辑器:gedit(TAB设置为4个空格)

Linux 操作回顾

# 创建文件 c
gedit test.c

# 编译文件 c
gcc test.c -o test

# 直接运行文件 c
./test

makefile 使用例子

test: test.c
	gcc test.c -o test
	

注意细节:

  1. target(test)冒号右边有个空格
  2. 第二行第一个符号是 TAB 键
  3. 最后一行存在,且是空的

运行 makefile 文件直接在 cmd 中输入 make 即可

多个文件 h 和 c 一起编译

main: main.c tool.o
	gcc main.c tool.o -o main
	
tool.o: tool.c
	gcc -c tool.c

clean:
	remove *.o main

多个文件 h 和多个文件 c 一起编译

.c 和 .h 分别代表实现和声明,如下:

// .c

#include<bar.h>
int find_min(int arr[] ,int n) {
    int m = arr[0];
    int i;
    for (i=0; i<n; ++i) {
        if(arr[i] < n)
            m = arr[i];
    }
}

// .h

int find_min(int arr[] ,int n);

makefile

CC = gcc
CFLAGS = -lm -Wall -g

all: main_max main_min

main_max: main_max.c tool.o
	$(CC) $(CFLAGS) main_max.c tool.o -o main_max
	
main_min: main_min.c tool.o
	$(CC) $(CFLAGS) main_min.c tool.o -o main_min

bar.o: bar.h
	$(CC) $(CFLAGS) -c bar.c

tool.o: tool.c
	$(CC) $(CFLAGS) -c tool.c

clean:
	remove *.o main_max main_min

执行

make clean
make
ls
喜欢划水摸鱼的废人
原文地址:https://www.cnblogs.com/CourserLi/p/15313273.html