1.代码结构
├── Makefile
├── dependent
│ └── test.c
└── main.c
2.Makefile
$(CC) = gcc
CFLAGS:= -Werror -std=c99
ENABLED_TEST:=true #通过控制此开关,达到是否加入自己代码部分
ifeq ($(strip $(ENABLED_TEST)),true)
CFLAGS+=-DENABLE_LOG #选择是否编译到源码
endif
SRC=./dependent/test.c
.PHONY:clean
all:main
main: main.c $(SRC)
@echo $@
@echo $<
@echo $^
$(CC) $(CFLAGS) -o $@ $^
clean:
rm main
注意:$@:目标文件
$^:依赖所有文件
3.test.c
# mkdir dependent
# cd dependent
# emacs test.c
#include <stdio.h>
int add(int a, int b){
#ifdef ENABLE_LOG
printf("%s(), line = %d, a = %d, b = %d\n",__FUNCTION__,__LINE__,a,b);
#endif
return (a + b);
}
4.main.c
#include <stdio.h>
extern int add(int a, int b);
int main(){
int m = 11;
int n = 12;
int sum;
sum = add(11,12);
#ifdef ENABLE_LOG
printf("%s(), line = %d, sum = %d\n",__FUNCTION__,__LINE__,sum);
#endif
}
来源:CSDN
作者:慢慢的燃烧
链接:https://blog.csdn.net/u010164190/article/details/103489495