makefile,因为宏定义函数无法自动检测改变的情况.解决方案

makefile,因为宏定义函数无法自动检测改变的情况.
不知道是我操作有问题还是makefile的bug,现在拿来大家一起看下这问题,大家也可以测试下:

1.首先我们准备三个测试文件,我把代码贴出来:
test.h
#define LOG_TYPE 2 //输出类型设置
#if(LOG_TYPE == 1) //1是输出到标准输出口
#define PRINTF(fmt, args …) printf(fmt, ##args)
#endif
#if(LOG_TYPE == 2) //2忽略调试信息输出
#define PRINTF(fmt, args …)
#endif
void test(void);
void test2(void);

test.c
#include <stdio.h>
#include "test.h"

#if(LOG_TYPE == 1)  
#define LOG_F(fmt, args …) printf(fmt, ##args)
#endif

#if(LOG_TYPE == 2)  
#define LOG_F(fmt, args …)
#endif

void test(void)
{
PRINTF("test PF \n");
LOG_F("test LOG_F \n");
}

void test2(void)
{
  #if(LOG_TYPE == 1)  
printf("test2 \n");
  #endif
  #if(LOG_TYPE == 2)  

  #endif
}

main.c

#include <stdio.h>
#include "test.h"

int main(void)
{
PRINTF("main !!\n");
test();
test2();
}

Makefile 
all:test
test:test.o main.o
gcc -o test test.o main.o

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

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

clean:
rm -f test.o main.o test

现在我们操作test.h文件中的宏LOG_TYPE:
1.设置LOG_TYPE = 1->make->./test 输出:
  main !!
  test PF  
  test LOG_F  
  test2
 
2.设置LOG_TYPE = 2->make->./test 输出: 
  main !!

注意按道理来说我们设置LOG_TYPE = 2就不应该在有输出main!!!的.

这说明函数连接的还是连接到上一个版本的test.o文件,这里为什么没有连接到最新的.o文件谁能分析下吗?

除非你全部make clean在make才不会发生这样的情况.


多看文档吧:
http://www.gnu.org/software/automake/manual/make/Automatic-Prerequisites.html


但还有一个错误就是单独执行make depend
会出现一个错误:make: Nothing to be done for `depend’.
—————————–
我不是很会makefile 这个不能算是错误吧
你去下载任意一个软件 直接就来一句make depend 也会出错的
make: *** ターゲット `depend’ を make するルールがありません. 中止.

makefile,因为宏定义函数无法自动检测改变的情况.解决方案

相关文章:

你感兴趣的文章:

标签云: