【问题标题】:Makefile not linking to one of the header files in one sub-directoryMakefile 未链接到一个子目录中的头文件之一
【发布时间】:2015-10-05 02:52:54
【问题描述】:

我正在尝试使用以下目录结构编写 Makefile -

示例 - 包含 Makefile、main.c、xyz.c、xyz.h 和子目录 HalInterrupt_Config
Hal - 包含 test2.ctest2.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 的任何地方使用 TRUEFALSE 的值并尝试执行 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 &lt;stdbool.h&gt;,您就会得到 C++ 名称bool&friends。
  • 可能是时候将所有文件压缩并发布到此处,以便其他人可以重现您的错误并查看问题所在。另外,您使用的是什么版本的 gcc 和什么操作系统?

标签: c makefile gnu-make


【解决方案1】:

我只是重写了包括 Makefile 在内的所有文件,这次它工作了,不知道是什么问题,因为一切似乎都还好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    相关资源
    最近更新 更多