本文演示配置makefile,检测修改的代码才编译,提升编译效率。
1. 测试代码
$ tree
.
├── app.c
├── lib
│ ├── module2.c
│ └── module2.h
├── makefile
├── module1.c
└── module1.h
app.c
#include <stdio.h>
#include "app.h"
#include "module1.h"
#include "lib/module2.h"
int main()
{
int a = 3;
int b = 2;
printf("c1 is %d\n", add(a, b));
printf("c2 is %d\n", sub(a, b));
return 0;
}
module1.h
int add(int a, int b);
module1.c
#include "module1.h"
int add(int a, int b)
{
return a + b;
}
lib/module2.h
int sub(int a, int b);
lib/module2.c
#include "module2.h"
int sub(int a, int b)
{
return a - b;
}
2. makefile
CC=gcc
SRCS=
SRCS+=app.c
SRCS+=module1.c
SRCS+=lib/module2.c
OBJECTS= $(patsubst %.c,%.o,${SRCS})
app : ${OBJECTS}
${CC} -o $@ $^
${OBJECTS}:%.o : %.c
${CC} -c $< -o $@
.PHONY:clean
clean:
rm -rf */*.o
rm -rf *.o
rm -rf app
标签:lib,修改,int,app,makefile,module2,module1,编译,include
From: https://www.cnblogs.com/Netsharp/p/17505843.html