我目前正在开发一个跨平台的 C 应用程序。
是否有任何编译器宏只在Windows上编译时定义,所以我可以#ifdef
某些特定于Windows的#includes?
典型的例子是在WinSock和Berkeley套接字标题之间进行选择:
#ifdef _WINDOWS
#include <winsock.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
所以我正在寻找的东西是那样的 _WINDOWS 宏。
答案 0 :(得分:10)
您最好的选择是使用
_WIN32
使用Visual C ++编译器编译32位或64位Windows平台时,guaranteed to be defined。我希望Windows的其他编译器也能定义它(英特尔C ++编译器定义它,MinGW gcc也是如此)。
答案 1 :(得分:3)
使用_WIN32
。
参考:
答案 2 :(得分:2)
_WIN32
为Win32和Win64的应用程序定义。始终定义。
_WIN64
为Win64的应用程序定义。
来源:Lists the predefined ANSI C and Microsoft C++ implementation macros.