简体   繁体   English

MinGW没有定义WIN32,预处理器指令中的错误

[英]MinGW not defining WIN32, error in preprocessor directives

I am using mingw32-make to compile a project to Windows, which depends on a project called libevent2. 我正在使用mingw32-make将项目编译到Windows,这依赖于一个名为libevent2的项目。 I keep receiving this error - 我一直收到这个错误 -

util.h:69:25: fatal error: sys/socket.h: No such file or directory

Obviously a file from the Linux API is trying to be included, which won't work on Windows. 显然,试图包含来自Linux API的文件,这在Windows上不起作用。 Upon further investigation I find however that this file should only be included if WIN32 isn't defined. 经过进一步调查,我发现只有在未定义WIN32时才应包含此文件

#ifdef WIN32
 #include <winsock2.h>
#else
 #include <sys/socket.h>
#endif

你应该使用_WIN32 ,也可能想检查__CYGWIN__

#if defined _WIN32 || defined __CYGWIN__

Are you sure there's nothing undefining WIN32 ? 你确定WIN32没有什么定义吗? My installation of MinGW (4.6.1 at this site) definitely defines it: 我在这个网站上安装MinGW(4.6.1)肯定定义了它:

C:\temp>gcc -E -dM test.c | find /i "win"
#define _WIN32 1
#define __WINT_MAX__ 65535
#define _WINT_T
#define __WINT_MIN__ 0
#define __WIN32 1
#define __WINNT 1
#define __WINNT__ 1
#define __WIN32__ 1
#define __SIZEOF_WINT_T__ 2
#define WIN32 1                 // <-- right here
#define __WINT_TYPE__ short unsigned int
#define WINNT 1

Try passing the -E -dM options to verify if your MinGW compiler is (or isn't) pre-defining the WIN32 macro. 尝试传递-E -dM选项以验证您的MinGW编译器是否(或不是)预定义WIN32宏。

Note that strictly speaking, WIN32 should not be predefined by the compiler (since it's in the user's namespace) - only _WIN32 should. 请注意,严格来说, WIN32 应该由编译器预定义(因为它在用户的命名空间中) - 只有_WIN32应该。 WIN32 should be set by the SDK being used and/or by the build environment - that's the way it works in Microsoft's compilers. WIN32应该由正在使用的SDK和/或构建环境设置 - 这就是它在Microsoft的编译器中的工作方式。

For example, there's the following sequence in windef.h " 例如, windef.h有以下顺序“

#ifndef WIN32
#define WIN32
#endif

and /D "WIN32" is put into Visual Studio C++ projects by default. /D "WIN32"默认放入Visual Studio C ++项目中。

See https://stackoverflow.com/a/662543/12711 for more details. 有关详细信息,请参阅https://stackoverflow.com/a/662543/12711

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

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