How do I update my compiler to use C++11 features?

倖福魔咒の 提交于 2019-12-11 05:14:59

问题


I tried to get myself into C++ and purchased the book "Programming - Principles and Practice Using C++" by Bjarne Stroustrup.

When I tried to get compile the following source code:

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

I am getting following compile error:

    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.

I get it that my compiler is not using C++11 feature, but I do not know how I can update the compiler. I should let know that I am using a MacOSX (10.10 Yosemite) and tried to compile with xCode and textmate. I even tried to follow this tutorial(https://wiki.helsinki.fi/display/HUGG/Installing+the+GNU+compilers+on+Mac+OS+X), but it did not helped. (At least when I tried to compile with text mate)

I hope you are able to help me. :(


回答1:


What you need to do is open the project settings -> Build Settings and set C++ Language Dialect to C++11 and the C++ Standard Library to libc++ (LLVM C++ standard library with C++11 support).




回答2:


If you are on a Mac or Linux, the compiler is usually g++ or clang; to access C++11, just specify -std=c++11 as an option when invoking the compiler (Assuming that you have an up-to-date version).




回答3:


From your build settings, adjust the C++ Language Dialect to C++11...

Also make sure you are using the LLVM C++ library, as it has full C++ 11 support.



来源:https://stackoverflow.com/questions/27385199/how-do-i-update-my-compiler-to-use-c11-features

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!