预处理器检查是否未定义多个定义

时间:2013-06-21 14:16:59

标签: c c-preprocessor

我在用户可编辑的标题中选择了#define,因此我随后希望在用户完全删除它们时检查这些定义是否存在,例如。

#if defined MANUF && defined SERIAL && defined MODEL
    // All defined OK so do nothing
#else
    #error "User is stoopid!"
#endif

这完全没问题,但我想知道是否有更好的方法来检查是否有多个定义没有到位......例如:

#ifn defined MANUF || defined SERIAL ||.... // note the n in #ifn

或者

#if !defined MANUF || !defined SERIAL ||....

删除空#if部分的需要。

2 个答案:

答案 0 :(得分:94)

#if !defined(MANUF) || !defined(SERIAL) || !defined(MODEL)

答案 1 :(得分:2)

FWIW,@ SergeyL的答案很棒,但这里有一个小小的测试版本。注意逻辑或逻辑的变化。

main.c有一个这样的主包装器:

#if !defined(TEST_SPI) && !defined(TEST_SERIAL) && !defined(TEST_USB)
int main(int argc, char *argv[]) {
  // the true main() routine.
}

spi.c,serial.c和usb.c有各自测试代码的主要包装器,如下所示:

#ifdef TEST_USB
int main(int argc, char *argv[]) {
  // the  main() routine for testing the usb code.
}

的config.h 所有c文件中包含的内容都包含如下条目:

// Uncomment below to test the serial
//#define TEST_SERIAL


// Uncomment below to test the spi code
//#define TEST_SPI

// Uncomment below to test the usb code
#define TEST_USB
相关问题