简体   繁体   English

C ++ #define值未在cpp文件中读取

[英]C++ #define value not being read in cpp file

In my C++ .h file: 在我的C ++ .h文件中:

class foo {
#define useThis true

...
}

In my .cpp file: 在我的.cpp文件中:

#if useThis
... generate A code
#else
... generate B code
#endif

The problem is that the #define values are not being read in the .cpp file, so what is happening is both A and B are being generated. 问题是在.cpp文件中未读取#define值,因此正在发生的是同时生成A和B。

I am including the .h file in the top of the .cpp file. 我将.h文件包含在.cpp文件的顶部。

Boolean value can not be used in macros for some compilers, like Visual Studio (works under g++ though). 对于某些编译器(例如Visual Studio),不能在宏中使用布尔值(尽管可以在g ++下工作)。 A cross compiler way should be: 交叉编译器的方式应为:

#define useThis 1

Or, define a macro without value, and use ifdef to test if it has been defined: 或者,定义一个没有值的宏,并使用ifdef来测试是否已定义:

#define useThis

#ifdef useThis
    ...
#else
    ...
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM