[英]C preprocessor #error in header file included in multiple source files
I have two source files, main.c and datamgr.c - and two header files, config.h and datamgr.h The testing system we're using expects these files, and only these files.我有两个源文件 main.c 和 datamgr.c - 以及两个头文件 config.h 和 datamgr.h 我们使用的测试系统需要这些文件,而且只有这些文件。
main.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:数据管理器.h:
#ifndef DATAMGR_H
#define DATAMGR_H
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
custom_type foo();
#endif
config.h:配置.h:
#ifndef CONFIG_H
#define CONFIG_H
#ifndef SET_MAX_TEMP
#error "Max temperature not set."
#endif
#ifndef SET_MIN_TEMP
#error "Max temperature not set."
#endif
typedef custom_type uint16_t
#endif
Now, the problem is that I can only define SET_MAX_TEMP
and SET_MIN_TEM
P in main.c, but both main.c and datamgr.c need both the header files.现在,问题是我只能在 main.c 中定义
SET_MAX_TEMP
和SET_MIN_TEM
P,但是 main.c 和 datamgr.c 都需要头文件。 So if I leave them undefined in datamgr.c I get a compiler error.因此,如果我在 datamgr.c 中未定义它们,则会出现编译器错误。 However, if I do define them in datamgr.c and later overwrite them in main.c, I get a different compiler error.
但是,如果我确实在 datamgr.c 中定义了它们,然后在 main.c 中覆盖它们,则会出现不同的编译器错误。
Please, any assistance as to how to get this horrible setup to work would be greatly appreciated.请,任何有关如何让这个可怕的设置工作的帮助将不胜感激。
您可以在编译时直接传递这些define
:
gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>
In datamgr.c do:在 datamgr.c 中:
#define SET_MAX_TEMP
#define SET_MIN_TEMP
#include "datamgr.h"
#include "config.h"
#undef SET_MAX_TEMP
#undef SET_MIN_TEMP
In a comment, you said:在评论中,您说:
Because main.c is the file that our testing system uses to implement the test scenarios.
因为 main.c 是我们的测试系统用来实现测试场景的文件。
In that case, make sure that the testing system defines those macros in the command line of the compiler for every file being compiled.在这种情况下,请确保测试系统在编译器的命令行中为每个正在编译的文件定义这些宏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.