C++ WIN32 在哪里定义,我如何在我的项目中包含这个定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5080233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is WIN32 defined, and how can I include this definition in my project?
提问by BeeBand
I am including a third party header and source file into my project.
我在我的项目中包含了第三方头文件和源文件。
At the top of the header there is this:
在标题的顶部有这个:
#if defined(WIN32) || defined(WIN16)
#ifndef MSDOS
#define MSDOS
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#ifndef MSDOS
#include <unistd.h>
#endif
#include "des.h"
The problem is that #if defined(WIN32) fails and the compilation fails when trying to #include unistd.h which I don't want to do.
问题是 #if defined(WIN32) 失败,并且在尝试 #include unistd.h 时编译失败,我不想这样做。
I have third party project that works with this header file i.e. WIN32 is defined and it doesn't try to include In Visual Studio I did "Go To Definition" on "WIN32" and was taken to the following definition in WinDefs.h.
我有使用这个头文件的第三方项目,即定义了 WIN32,它没有尝试包含在 Visual Studio 中,我在“WIN32”上做了“转到定义”,并被带到了 WinDefs.h 中的以下定义。
#define WIN32
I'm not sure this is where its getting WIN32 definition from, as the third party project does not appear to include "WinDefs.h".
我不确定这是从哪里获取 WIN32 定义的,因为第三方项目似乎不包含“WinDefs.h”。
So my problem is, how can I get WIN32 to be defined in my current new project?
所以我的问题是,如何在我当前的新项目中定义 WIN32?
采纳答案by Axel
Depends on your project setup. WIN32 is defined inside the windows header files, but you can pass it to the compiler as well ("-DWIN32" for gcc for example). Try it and see whether it compiles.
取决于您的项目设置。WIN32 是在 windows 头文件中定义的,但您也可以将其传递给编译器(例如,gcc 的“-DWIN32”)。尝试一下,看看它是否编译。
回答by patthoyts
Visual Studio has the built-in define _WIN32. mingw-gcc has WIN32 and _WIN32 built-in so the project was likely tested using gcc. You might add
Visual Studio 具有内置定义 _WIN32。mingw-gcc 内置了 WIN32 和 _WIN32,因此该项目可能使用 gcc 进行了测试。你可能会添加
#if defined(_WIN32) && !defined(WIN32)
#define WIN32
#endif
or just add a -DWIN32 to the CFLAGS.
或者只是在 CFLAGS 中添加一个 -DWIN32。
回答by Default
Check your includes. I am guessing that the third party header is included prior to the windows.h. So, in your main.cpp or equal it should be
检查您的包含。我猜第三方标头包含在 windows.h 之前。所以,在你的 main.cpp 或等于它应该是
#include <windows.h> // this will also include windefs.h
#include <thirdParty.h>
and not the other way around.
而不是相反。
Hope that helps.
希望有帮助。
回答by user3731622
For those seeking answers to the
对于那些寻求答案的人
where is WIN32 defined
WIN32 在哪里定义
part of the questions, I've found it defined in:
部分问题,我发现它定义在:
minwindef.h
ole2.h
minwindef.h
ole2.h
Note, I have no confidence that these are the only places it's defined. I expect there are probably other files where it's defined. Nevertheless, I thought this might help some people.
请注意,我不相信这些是它定义的唯一地方。我希望可能还有其他定义它的文件。尽管如此,我认为这可能会帮助一些人。
回答by 15 С
Some WIN32 defined in the compiler . Just like this,If you use the gcc for windows , WIN32 is defined . If you use the gcc for linux , WIN32 is not defined :)
So , the macros is a switch. You can define it to use somethine , and not define it to unuse something.
一些WIN32在编译器中定义。就这样,如果你用windows的gcc,WIN32就定义好了。如果您使用 gcc for linux,则未定义 WIN32 :)
因此,宏是一个开关。您可以将其定义为使用 somethine ,而不是将其定义为不使用某物。
回答by floyd73
You can simply include the windows header files (windows.h) before including the third party header - as you already found out WIN32 is defined there but technicaly it could be defined anywhere (so if the third party project is not including the windows headers check if it's being defined in the compiler project settins directly).
您可以在包含第三方头文件之前简单地包含 Windows 头文件 (windows.h) - 正如您已经发现 WIN32 在那里定义的那样,但技术上它可以在任何地方定义(所以如果第三方项目不包括 Windows 头文件,请检查如果它是直接在编译器项目设置中定义的)。
BTW there is also a _WIN32 define that is set by the compiler, it's possibly a better idea to look for this define if checking if the code is being compiled under windows;
顺便说一句,还有一个由编译器设置的 _WIN32 定义,如果检查代码是否在 windows 下编译,则查找此定义可能是一个更好的主意;