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

Num*_*our 5 c cross-platform platform-specific include c-preprocessor

我目前正在开发一个跨平台的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
Run Code Online (Sandbox Code Playgroud)

所以我正在寻找的东西就像那个_WINDOWS宏.

Jam*_*lis 10

你最好的选择是使用

_WIN32
Run Code Online (Sandbox Code Playgroud)

在使用Visual C++编译器编译32位或64位Windows平台时,可以保证定义它.我希望Windows的其他编译器也可以定义它(英特尔C++编译器定义它,MinGW gcc也是如此).

  • 32实际上是将它与古老的16位版本的Windows分开.在Win64中删除该定义会破坏太多东西,因此将其留下来不那么麻烦. (2认同)