简体   繁体   中英

GCC C++11 Fails to See #if windows And #if linux (Re-ask)

When using the #if windows/#if linux compilers features in Debian GCC version 4.7.2, I have been unable to get them work when using std11.

Independently, I can get the compiler to accept the if-defines without complaint. I can also get the compiler to use the same code with c++ 11, without any if-defines (and thus not conditionally). But when I run the conditional defines through the same compiler, with the tag for c++ 11, the code is rejected.

Below I have included a simple example, with two alternate mains, and the error I get. The only difference between the two mains are the commented out lines.

Runs:

g++ main.cpp -std=c++11 -o test

The above uses the c++ 11 standard. When running it works perfectly. 它可以完美运行。 But when running , it fails entirely, giving the error at the far end of this post. ,它将完全失败,从而在本文的末尾给出错误。

g++ main.cpp -o test

The above does not use the c++ 11 standard. When running either or it works perfectly. 或未它可以完美运行。

Below are the code examples.

:

#include <iostream>

//#if windows
//#include "WindowsSolution.hpp"
//#endif

//#if linux
#include "LinuxSolution.hpp"
//#endif

int main()
{
    std::cout << myGlobalSolution.out() << std::endl;
    return 0;
}

:

class LinSolution{
public:
    LinSolution(){

    }
    std::string out(){
        std::string ret("Linux solution");
        return ret;
    }
};
LinSolution myGlobalSolution;

:

class WinSolution{
private:
    WinSolution(){

    }
    std::string out(){
        std::string ret("Windows solution");
        return ret;
    }
};
WinSolution myGlobalSolution;

:

#include <iostream>

#if windows
#include "WindowsSolution.hpp"
#endif

#if linux
#include "LinuxSolution.hpp"
#endif

int main()
{
    std::cout << myGlobalSolution.out() << std::endl;
    return 0;
}

Below is the error I get when compiling with the , using the c++ 11 flag. 进行编译时遇到的错误。

main.cpp: In function ‘int main()’:
main.cpp:13:15: error: ‘myGlobalSolution’ was not declared in this scope

The simple constant linux is a GCC extension and not an official OS constant. The proper constant for Debian is probably __gnu_linux__ ; you can find a list of them for various systems here . Usually official predefined constants follow the convention of using __ at the start.

Your code works without the standard flag because by default GCC compiles in a GNU language mode (GNU C++) rather than a standard language mode (ISO C++); the GNU language includes extensions (extra language features, and in this case, legacy OS constants). When you pass the -std=c++11 flag you are requesting ISO language mode, which means GNU extensions are disabled , including GNU-only constants. To get both GNU extensions and a specific set of ISO features, try requesting a GNU language standard version instead (eg -std=gnu++11 works fine).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM