【发布时间】:2015-10-05 02:52:54
【问题描述】:
我正在尝试使用以下目录结构编写 Makefile -
示例 - 包含 Makefile、main.c、xyz.c、xyz.h 和子目录 Hal 和 Interrupt_Config
Hal - 包含 test2.c 和 test2.h
Interrupt_Config - 包含 try.h
下面是我正在使用的 Makefile -
EXE := practice
CC := gcc
CPPFLAGS := -IHal -IInterrupt_Config
VPATH := Hal:Interrupt_Config
MAIN_OBS := $(patsubst %.c,%.o,$(wildcard *.c))
INT_OBS := $(patsubst %.c,%.o,$(wildcard Interrupt_Config/*.c))
HAL_OBS := $(patsubst %.c,%.o,$(wildcard Hal/*.c))
ALL_DEPS := $(patsubst %.o,%.d,$(MAIN_OBS), $(HAL_OBS), $(INT_OBS))
all: $(EXE)
$(EXE): $(MAIN_OBS) $(HAL_OBS) $(INT_OBS)
$(CC) -o $@ $(LDFLAGS) $^ $(LDLIBS)
%.o: %.c
$(CC) -o $@ -MD -MP $(CPPFLAGS) $(CFLAGS) -c $<
-include $(ALL_DEPS)
clean:
rm -f $(EXE) $(MAIN_OBS) $(INT_OBS) $(HAL_OBS) $(ALL_DEPS)
.PHONY: all clean
每当我尝试在不包含 Interrupt_Config/try.h 的情况下执行 make 时,它都可以正常工作。我的 main.c 包含是这样的 -
..
#include "test2.h"
#include "xyz.h"
#include "try.h" // Problem is here
..
而try.h就是这么简单——
#ifndef TRY_H
#define TRY_H
#define TRUE 1
#define FALSE 0
#endif
但是当我在 main.c 的任何地方使用 TRUE 或 FALSE 的值并尝试执行 make 再次。它向我抛出错误 - 'TRUE' 未定义(在此函数中首次使用)和类似的 'FALSE'。
我无法理解究竟是什么问题,因为我是 Makefile 新手。
编辑
main.c
#include <stdio.h>
#include <stdlib.h>
#include "test2.h"
#include "try.h"
#include "xyz.h"
int main()
{
bugs(); //test2.h
bunny(); //test2.h
t_print(); // xyz.h
printf("Yes! your age is %d\n",TRUE); // try.h
}
【问题讨论】:
-
是不是说找不到
try.h? -
您在任何其他头文件中都没有 TRY_H 作为多重包含保护吗?
-
将
#message或#warning添加到try.h以确保包含它。如果是,则有人在包含后某处未定义 FALSE 和 TRUE.. -
你真的知道从 C99 开始在 C 中有一个布尔类型吗?只需
#include <stdbool.h>,您就会得到 C++ 名称bool&friends。 -
可能是时候将所有文件压缩并发布到此处,以便其他人可以重现您的错误并查看问题所在。另外,您使用的是什么版本的 gcc 和什么操作系统?