C预处理器#error in header file included in multiple source files
C preprocessor #error in header file included in multiple source 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:
#ifndef DATAMGR_H
#define DATAMGR_H
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
custom_type foo();
#endif
config.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
现在,问题是我只能在main.c中定义SET_MAX_TEMP
和SET_MIN_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
:
gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>
在评论中,您说:
Because main.c is the file that our testing system uses to implement the test scenarios.
在这种情况下,请确保测试系统在编译器的命令行中为每个正在编译的文件定义了这些宏。
我有两个源文件,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:
#ifndef DATAMGR_H
#define DATAMGR_H
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
custom_type foo();
#endif
config.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
现在,问题是我只能在main.c中定义SET_MAX_TEMP
和SET_MIN_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
:
gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>
在评论中,您说:
Because main.c is the file that our testing system uses to implement the test scenarios.
在这种情况下,请确保测试系统在编译器的命令行中为每个正在编译的文件定义了这些宏。