简体   繁体   English

如何在Visual C ++中有条件地包含不存在的文件?

[英]How can I conditionally include a nonexistent file in Visual C++?

I'm using Visual C++ to build and test a project, but I'll also be using it on other platforms. 我正在使用Visual C ++来构建和测试项目,但我也将在其他平台上使用它。 I started the code initially on a different platform. 我最初在不同的平台上启动了代码。

In some of my class headers I need to include a file which is specific to the other platform, however, this file doesn't exist in the Visual C++ workspace. 在我的一些类头文件中,我需要包含一个特定于另一个平台的文件,但是,该文件在Visual C ++工作区中不存在。

I've tried using code like this: 我试过使用这样的代码:

 #if TARGET == OTHERPLATFORM
 #include "themissingfile.h"
 #endif

where TARGET is defined elsewhere as WINDOWS 其中TARGET在其他地方被定义为WINDOWS

 #define TARGET WINDOWS

However, I still get a compiler error stating that "themissingfile.h" cannot be found. 但是,我仍然收到编译器错误,指出无法找到“themissingfile.h”。 It appears like the precompiler is processing the include before the if. 看起来好像预编译器正在处理if之前的include。 What's the best way to get around this? 什么是最好的解决方法? I suppose I could just create a blank "themissingfile.h" in the Visual C++ source folder, but it seems to me like there should be a better solution. 我想我可以在Visual C ++源文件夹中创建一个空白的“themissingfile.h”,但在我看来应该有一个更好的解决方案。

Use _WIN32 macro instead: 请改用_WIN32宏:

#ifndef _WIN32
#include "themissingfile.h"
#endif

_WIN32 is defined for both 32-bit and 64-bit builds. _WIN32是为32位和64位版本定义的。 See C/C++ Predefined Macros . 请参阅C / C ++预定义宏

#define TARGET WINDOWS does not set TARGET to the string WINDOWS: it sets it to whatever WINDOWS is defined to. #define TARGET WINDOWS没有将TARGET设置为字符串WINDOWS:它将它设置为WINDOWS定义的任何内容。 Most likely this happens: 很可能发生这种情况:

  #define WINDOWS          // Sets WINDOWS to 0
  #define OTHERPLATFORM    // Sets OTHERPLATFORM to 0
  #define TARGET WINDOWS   // Sets TARGET to 0

  #if TARGET == OTHERPLATFORM // Evaluates to 0==0, so true

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

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