0

in my centos7.0 system, when i compile the code with g++ test.cpp -o test, the output is "is linux system"

when compile with g++ test.cpp -std=c++11 -o test the output is "is not linux system"

why in c11 the compiler default macro changed?

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;
}

1 Answers1

0

linux is not a reserved identifier according to ISO C or C++ standards (all versions), so it is not allowed to be predefined. Hence the behaviour under -std=c++11.

The behaviour with no std switch is a GNU variant which does not conform to ISO standards, in which linux is defined (along with various other things).

See this thread for some ideas about detecting the system while complying with ISO standards. Perhaps __linux__ will be suitable. Another option would be to use -std=gnu++11, which is a different GNU variant that includes some C++11 features.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365