在Windows环境下,可以使用预定义的宏来判断代码是否正在编译到Windows。以下是一些常用的宏:
_WIN32
: 当编译到Windows 32位系统时定义。_WIN64
: 当编译到Windows 64位系统时定义。WINVER
: 定义Windows的版本号,例如0x0601
表示Windows 7。_WIN32_WINNT
: 定义正在运行的Windows版本的最低要求,例如0x0601
表示Windows 7。可以在代码中使用这些宏来判断当前编译环境是否为Windows,例如:
#ifdef _WIN32
printf("Compiling on Windows 32-bit\n");
#elif defined(_WIN64)
printf("Compiling on Windows 64-bit\n");
#else
printf("Not compiling on Windows\n");
#endif
这段代码将会根据编译环境输出不同的信息。如果编译环境是Windows 32位或64位,则会输出相应的信息,否则会输出"Not compiling on Windows"。
需要注意的是,这些宏只能用于判断编译环境是否为Windows,而不能用于判断运行环境是否为Windows。如果需要在运行时判断操作系统类型,可以使用操作系统提供的API,例如Windows中的GetVersionEx
函数。
领取专属 10元无门槛券
手把手带您无忧上云