GCC C ++ 11无法看到#if Windows和#if linux(重新询问) [英] GCC C++11 Fails to See #if windows And #if linux (Re-ask)

查看:333
本文介绍了GCC C ++ 11无法看到#if Windows和#if linux(重新询问)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Debian GCC版本4.7.2中使用#if Windows/#if linux编译器功能时,使用std11时我无法使它们正常工作.

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.

我可以独立地让编译器毫无疑问地接受if定义.我还可以使编译器在c ++ 11中使用相同的代码,而没有任何if-defines(因此不是有条件的).但是,当我通过同一编译器运行条件定义时,使用带有c ++ 11的标记,则代码将被拒绝.

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.

运行:

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

以上使用c ++ 11标准.当运行 commented main 时,它可以完美运行.但是,当运行 uncommented main 时,它会完全失败,并在本文的尽头给出错误.

The above uses the c++ 11 standard. When running commented main it works perfectly. But when running uncommented main, it fails entirely, giving the error at the far end of this post.

g++ main.cpp -o test

以上内容不使用c ++ 11标准.当运行有注释的主程序无注释的主程序时,它运行完美.

The above does not use the c++ 11 standard. When running either commented main or uncommented main it works perfectly.

下面是代码示例.

注释main.cpp :

#include <iostream>

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

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

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

LinuxSolution.hpp :

class LinSolution{
public:
    LinSolution(){

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

WindowsSolution.hpp :

class WinSolution{
private:
    WinSolution(){

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

未注释的main.cpp :

#include <iostream>

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

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

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

以下是我在使用c ++ 11标志使用未注释的main.cpp 进行编译时遇到的错误.

Below is the error I get when compiling with the uncommented main.cpp, using the c++ 11 flag.

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

推荐答案

简单常量linux是GCC扩展,不是正式的OS常量. Debian的适当常数可能是__gnu_linux__;您可以在此处找到它们的列表.通常,正式的预定义常量遵循一开始就使用__的约定.

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.

您的代码无需标准标志即可工作,因为默认情况下,GCC会以GNU语言模式(GNU C ++)而不是标准语言模式(ISO C ++)进行编译; GNU语言包括扩展名(其他语言功能,在这种情况下为旧版OS常数).传递-std=c++11标志时,您正在请求ISO语言模式,这意味着禁用了 扩展,包括仅GNU常量.要同时获得GNU扩展和一组特定的ISO功能,请尝试请求一个GNU语言标准版本(例如-std=gnu++11可以正常工作).

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 (e.g. -std=gnu++11 works fine).

这篇关于GCC C ++ 11无法看到#if Windows和#if linux(重新询问)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆