【问题标题】:How do I update my compiler to use C++11 features?如何更新我的编译器以使用 C++11 功能?
【发布时间】:2014-12-09 17:34:56
【问题描述】:

我试图让自己进入 C++ 并购买了 Bjarne Stroustrup 的《编程 - 使用 C++ 的原则和实践》一书。

当我尝试编译以下源代码时:

#include "std_lib_facilities.h"
int main(){
    cout<<"Hello, World!\n";
    keep_window_open();
    return 0;
}

我收到以下编译错误:

    In file included from /Users/hypertrooper/Documents/Programming - Principles and Practice Using C++/hello_world.cpp:1:
std_lib_facilities.h:71:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
        using size_type = typename std::vector<T>::size_type;
                          ^
std_lib_facilities.h:102:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
        using size_type = std::string::size_type;
                          ^
std_lib_facilities.h:107:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
                if (i<0||size()<=i) throw Range_error(i);
                    ~^~
std_lib_facilities.h:113:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
                if (i<0||size()<=i) throw Range_error(i);
                    ~^~
std_lib_facilities.h:213:107: error: expected '(' for function-style cast or type construction
inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
                                                                                ~~~~~~~~~~~~~~~~~~~~~~~~~~^
std_lib_facilities.h:222:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Value_type = typename C::value_type;
                   ^
std_lib_facilities.h:225:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Iterator = typename C::iterator;
                 ^
6 warnings and 1 error generated.

我知道我的编译器没有使用 C++11 功能,但我不知道如何更新编译器。我应该让我知道我正在使用 MacOSX (10.10 Yosemite) 并尝试使用 xCode 和 textmate 进行编译。我什至尝试遵循本教程(https://wiki.helsinki.fi/display/HUGG/Installing+the+GNU+compilers+on+Mac+OS+X),但没有帮助。 (至少当我尝试用文本伴侣编译时)

我希望你能帮助我。 :(

【问题讨论】:

    标签: c++ xcode c++11 textmate


    【解决方案1】:

    如果你使用的是 Mac 或 Linux,编译器通常是 g++ 或 clang;要访问 C++11,只需在调用编译器时指定 -std=c++11 作为选项(假设您有最新版本)。

    【讨论】:

    • @Macmade:这不一定是真的。例如,如果您通过自制软件安装 gcc。
    【解决方案2】:

    您需要做的是打开项目设置 -> 构建设置并将 C++ 语言方言设置为 C++11,将 C++ 标准库设置为 libc++(支持 C++11 的 LLVM C++ 标准库)。

    【讨论】:

      【解决方案3】:

      从您的构建设置中,将 C++ Language Dialect 调整为 C++11...

      还要确保您使用的是 LLVM C++ 库,因为它完全支持 C++ 11。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-18
        • 2012-04-16
        • 2013-07-20
        • 1970-01-01
        • 2011-11-14
        • 1970-01-01
        相关资源
        最近更新 更多