Where is WIN32 defined, and how can I include this definition in my project?

余生颓废 提交于 2020-01-21 06:32:08

问题


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.

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.

#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".

So my problem is, how can I get WIN32 to be defined in my current new project?


回答1:


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.




回答2:


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


#if defined(_WIN32) && !defined(WIN32)
#define WIN32
#endif

or just add a -DWIN32 to the CFLAGS.




回答3:


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

#include <windows.h> // this will also include windefs.h
#include <thirdParty.h>

and not the other way around.

Hope that helps.




回答4:


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).

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;




回答5:


For those seeking answers to the

where is WIN32 defined

part of the questions, I've found it defined in:

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.




回答6:


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.



来源:https://stackoverflow.com/questions/5080233/where-is-win32-defined-and-how-can-i-include-this-definition-in-my-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!