Which Cross Platform Preprocessor Defines? (__WIN32__ or __WIN32 or WIN32 )?(哪个跨平台预处理器定义?(__WIN32__ 或 __WIN32 或 WIN32 )?)
问题描述
我经常看到 __WIN32、WIN32 或 __WIN32__.我认为这取决于使用的预处理器(来自 Visual Studio 或 gcc 等).
我现在必须先检查 os,然后再检查使用的编译器吗?我们在这里使用 G++ 4.4.x、Visual Studio 2008 和 Xcode(我假设它又是一个 gcc)和 ATM,我们只使用 __WIN32__、__APPLE__ 和 __LINUX__.
这取决于你想要做什么.如果您的程序想要使用某些特定功能(例如来自 gcc 工具链),您可以检查编译器.如果您想使用某些操作系统特定的功能(无论编译器如何 - 例如 Windows 上的 CreateProcess 和 unix 上的 fork),您可以检查操作系统(_WINDOWS、__unix__).
Visual C 的宏p>
gcc 的宏
您必须检查每个编译器的文档,以便能够在编译时检测到差异.我记得 gnu 工具链(gcc)在 C 库(libc)中有一些函数不在其他工具链上(例如 Visual C).这样,如果您想在商品之外使用这些功能,那么您必须检测到您正在使用 GCC,因此您必须使用的代码如下:
#ifdef __GNUC__//做我的 gcc 特定的东西#别的//... 为其他编译器处理这个#万一I often see __WIN32, WIN32 or __WIN32__. I assume that this depends on the used preprocessor (either one from visual studio, or gcc etc).
Do I now have to check first for os and then for the used compiler? We are using here G++ 4.4.x, Visual Studio 2008 and Xcode (which I assume is a gcc again) and ATM we are using just __WIN32__, __APPLE__ and __LINUX__.
It depends what you are trying to do. You can check the compiler if your program wants to make use of some specific functions (from the gcc toolchain for example). You can check for operating system ( _WINDOWS, __unix__ ) if you want to use some OS specific functions (regardless of compiler - for example CreateProcess on Windows and fork on unix).
Macros for Visual C
Macros for gcc
You must check the documentation of each compiler in order to be able to detect the differences when compiling. I remember that the gnu toolchain(gcc) has some functions in the C library (libc) that are not on other toolchains (like Visual C for example). This way if you want to use those functions out of commodity then you must detect that you are using GCC, so the code you must use would be the following:
#ifdef __GNUC__
// do my gcc specific stuff
#else
// ... handle this for other compilers
#endif
这篇关于哪个跨平台预处理器定义?(__WIN32__ 或 __WIN32 或 WIN32 )?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:哪个跨平台预处理器定义?(__WIN32__ 或 __WIN32 或 WIN32 )?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
