确定是在Windows上还是在其他系统上进行编译

时间:2010-04-05 15:18:46

标签: c include cross-platform c-preprocessor platform-specific

我目前正在开发一个跨平台的 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 宏。

3 个答案:

答案 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.