哪个跨平台预处理器定义?(__WIN32__ 或 __WIN32 或 WIN32 )? [英] Which Cross Platform Preprocessor Defines? (__WIN32__ or __WIN32 or WIN32 )?

查看:21
本文介绍了哪个跨平台预处理器定义?(__WIN32__ 或 __WIN32 或 WIN32 )?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到 __WIN32WIN32__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 )?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆