提问者:小点点

在未定义的内容上使用#if时出现编译器警告/错误


请考虑以下示例:

#define FOO false
#if FOO
void foo(){}
#endif

显然,foo()并不存在。 但是当我忘记定义foo时,结果是一样的:

#if FOO
void foo(){}
#endif

有没有办法根据foo有条件地设置foo(){},但在根本没有定义foo时自动产生警告或错误? (即无需手动使用类似#ifndef FOO-#error)


共1个答案

匿名用户

您可以执行以下操作:

#if defined(FOO)
void foo() {}
#else
#error FOO is not defined
#endif

如果未定义foo,则不会编译此代码。 编译器将报告错误。