C预处理器#多个源文件中包含的头文件出错

C预处理器#多个源文件中包含的头文件出错,c,include,c-preprocessor,C,Include,C Preprocessor,我有两个源文件main.c和datamgr.c,还有两个头文件config.h和datamgr.h 我们正在使用的测试系统需要这些文件,而且只有这些文件 主要条款c: #include "datamgr.h" #include "config.h" int main() { custom_type a = 1; a = foo(); return 0; } datamgr.c: #include "datamgr.h" #include "config.h" cu

我有两个源文件main.c和datamgr.c,还有两个头文件config.h和datamgr.h 我们正在使用的测试系统需要这些文件,而且只有这些文件

主要条款c:

#include "datamgr.h"
#include "config.h"

int main() {
    custom_type a = 1;

    a = foo();
    return 0;
}
datamgr.c:

#include "datamgr.h"
#include "config.h"

custom_type foo() {
    custom_type a = 1;
    return a;
}
datamgr.h:

#ifndef DATAMGR_H
#define DATAMGR_H

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

custom_type foo();

#endif
现在,问题是我只能在main.c中定义
SET\u MAX\u TEMP
SET\u MIN\u TEM
p,但是main.c和datamgr.c都需要这两个头文件。因此,如果我在datamgr.c中未定义它们,我将得到一个编译器错误。但是,如果我在datamgr.c中定义它们,然后在main.c中覆盖它们,则会出现不同的编译器错误

请提供任何帮助,以使这个可怕的设置正常工作,我们将不胜感激。

在datamgr.c中,请执行以下操作:

#define SET_MAX_TEMP
#define SET_MIN_TEMP

#include "datamgr.h"
#include "config.h"

#undef SET_MAX_TEMP
#undef SET_MIN_TEMP

您可以在编译时直接传递这些
define
s:

gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>
gcc-DSET\u MAX\u TEMP-DSET\u MIN\u TEMP

在评论中,您说:


因为main.c是我们的测试系统用来实现测试场景的文件


在这种情况下,请确保测试系统在编译器的命令行中为每个正在编译的文件定义了这些宏。

为什么只能在main.c中定义它们?您应该能够在头文件(仅)中定义它们,并且它将在所有文件中工作。因为main.c是我们的测试系统用于实现测试场景的文件。因此,如果它们在main.c->error中未定义。测试系统没有触及datamgr.c和两个头文件。我应该更清楚,测试系统是为我们的大学测试代码的功能。“它是不能改变的。”fakeskuH听起来像是你需要和老师的助手谈谈,问问你该怎么做。明智的解决方案是在config.h或datamgr.h中定义最大值和最小值。如果他们想让你做一些不同的事情,那么他们需要告诉你那是什么。
gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>