windows 基于操作系统的C++条件编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/904745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional compilation in C++ based on operating system
提问by M. Dudley
I would like to write a cross-platform function in C++ that contains system calls. What conditional compilation flags can I check to determine which operating system the code is being compiled for? I'm interested mostly in Windows and Linux, using Visual Studio and GCC.
我想用 C++ 编写一个包含系统调用的跨平台函数。我可以检查哪些条件编译标志来确定代码是为哪个操作系统编译的?我最感兴趣的是 Windows 和 Linux,使用 Visual Studio 和 GCC。
I think it should look something like this:
我认为它应该是这样的:
void SomeClass::SomeFunction()
{
// Other code
#ifdef LINUX
LinuxSystemCall();
#endif
#ifdef WINDOWS
WindowsSystemCall();
#endif
// Other code
}
回答by Thilo-Alexander Ginkel
My gcc (4.3.3) defines the following Linux-related predefined macros:
我的 gcc (4.3.3) 定义了以下与 Linux 相关的预定义宏:
$ gcc -dM -E - < /dev/null | grep -i linux
#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define linux 1
Under VC++ (and many other Win32 compilers) there are also a couple of predefined macros identifying the platform, most notably _WIN32. Further details: http://msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx
在 VC++(和许多其他 Win32 编译器)下,还有几个预定义的宏标识平台,最显着的是 _WIN32。更多详情:http: //msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx
回答by JaredPar
There is no standard way to do this. It may be possible to key off certain macros that are defined per platform. For instance, _WIN32 will be defined on Windows and almost certainly not Linux. However I don't know of any corresponding Linux macro.
没有标准的方法可以做到这一点。可以关闭每个平台定义的某些宏。例如,_WIN32 将在 Windows 上定义,几乎肯定不会在 Linux 上定义。但是我不知道任何相应的 Linux 宏。
Because you are using separate compilers, it follows that you have separate build environments. Why not just add the macro yourself? Both Visual Studio and GCC support defining macros from the command line so just define them.
因为您使用单独的编译器,所以您有单独的构建环境。为什么不自己添加宏?Visual Studio 和 GCC 都支持从命令行定义宏,因此只需定义它们即可。
回答by AShelly
I always try to keep the platform specifics out of the main code by doing it this way
我总是尝试通过这种方式将平台细节排除在主要代码之外
platform.h:
平台.h:
#if BUILD_PLATFORM == WINDOWS_BUILD
#include "windows_platform.h"
#elif BUILD_PLATFORM == LINUX_BUILD
#include "linux_platform.h"
#else
#error UNSUPPORTED PLATFORM
#endif
someclass.c:
someclass.c:
void SomeClass::SomeFunction()
{
system_related_type t;
// Other code
platform_SystemCall(&t);
// Other code
}
Now in windows_platform.h
and linux_platform.h
you typedef system_related_type
to the native type, and either #define platform_SystemCall
as the native call, or create a small wrapper function, if the argument set from one platform to the other is too different.
现在,在windows_platform.h
与linux_platform.h
你的typedefsystem_related_type
原生类型,无论是#define platform_SystemCall
作为本地通话,或创建一个小包装的功能,如果该参数设置从一个平台到另一个是太不一样了。
If the system APIs for a particular task are wildly different between platforms, you may need to create your own version API that splits the difference. But for the most part, there are fairly direct mappings between the various APIs on Windows and Linux.
如果特定任务的系统 API 因平台而异,您可能需要创建自己的版本 API 来拆分差异。但在大多数情况下,Windows 和 Linux 上的各种 API 之间存在相当直接的映射。
Rather than relying on some particular compiler #define to select platform, I #define BUILD_PLATFORM xxx
in the project file or makefile, since those have to be unique by platform anyway.
而不是依赖某些特定的编译器 #define 来选择平台,我#define BUILD_PLATFORM xxx
在项目文件或 makefile 中,因为无论如何这些都必须是平台唯一的。
回答by Contango
Here's what I use. It works under Visual Studio 2008, and MinGW:
这是我使用的。它在 Visual Studio 2008 和 MinGW 下工作:
#ifdef __GNUC__
#define LINUX
#else
#define WINDOWS
#endif
#ifdef WINDOWS
#include "stdafx.h"
#else
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <malloc.h>
#endif