简体   繁体   English

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

[英]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_TEMPSET_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.

相关问题 多个应用程序 header 文件在 C 中包含错误 - Multiple application header files included error in C 为什么在我的C头文件中包含freetype头文件会产生错误,但可以包含在.c文件中? - Why does including the freetype header files in my C header file produce an error, but can be included in the .c file? C文件包含的所有头文件的列表 - list of all header files included by a C file 如何在C预处理器中包含多个目录以搜索头文件 - How to include multiple directories to search header files in C preprocessor 包含头文件中的C预处理器 - C preprocessor in include header files 在C中的多个源文件中包含头文件 - Including header files in multiple source files in C 将多个头文件转换为一个源文件? - Multiple header files to one source file? 在源文件中获取对特定函数的所有调用并生成其他文件(使用C,C ++预处理器或脚本) - Get all calls to specific function in source file and generate other files (using C, C++ preprocessor or scripts) 多个源文件和头文件:包括 .c 和 .h 文件 - Multiple source and header files: including .c and .h files 如何在不扩展包含的头文件的情况下预编译C源文件? - How to pre-compile a C source file without expand the included header file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM