Android/Linux编译开关使用

女生的网名这么多〃 提交于 2019-12-11 12:08:10
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
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!