本文介绍了基于操作系统的C ++中的条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C ++编写一个包含系统调用的跨平台函数.我可以检查哪些条件编译标志来确定要为其编译代码的操作系统?我主要对使用Visual Studio和GCC的Windows和Linux感兴趣.

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.

我认为它应该看起来像这样:

I think it should look something like this:

void SomeClass::SomeFunction()
{
    // Other code

#ifdef LINUX
    LinuxSystemCall();
#endif

#ifdef WINDOWS
    WindowsSystemCall();
#endif

    // Other code
}

推荐答案

我的gcc(4.3.3)定义了以下与Linux相关的预定义宏:

My gcc (4.3.3) defines the following Linux-related predefined macros:

$ gcc -dM -E - < /dev/null | grep -i linux
#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define linux 1

在VC ++(和许多其他Win32编译器)下,还存在几个用于标识平台的预定义宏,最著名的是_WIN32.更多详细信息: http://msdn.microsoft.com/en -us/library/b0084kay(VS.80).aspx

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

这篇关于基于操作系统的C ++中的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:42
查看更多