2016-10-11 72 views
0

在我的系统centos7.0, 当我编译使用g ++ TEST.CPP -o测试, 输出 “是Linux系统”克++在C11编译器宏

当编译使用g ++ TEST.CPP代码-std = C++ 11 -o test 输出是“不是linux系统”

为什么在c11中编译器的默认宏改变了?

TEST.CPP

#include <iostream> 
using namespace std; 
int main(){ 
    #ifdef linux 
     cout<<"is linux system\n"; 
    #else 
     cout<<"is not linux system\n"; 
    #endif 
    return 0; 
} 

回答

0

linux不是根据ISO C或C++标准(所有版本)中的保留标识符,因此它是不允许被预定义。因此在-std=c++11下的行为。

没有std开关的行为是一种GNU变体,它不符合ISO标准,其中linux被定义(以及其他各种事物)。

See this thread有关检测系统,同时遵守ISO标准的一些想法。也许__linux__将是合适的。另一个选择是使用-std=gnu++11,这是一个不同的GNU变体,包含一些C++ 11功能。