Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]Make QtCreator, QMake and clang3.2 work with c++11



  • After some struggle, I set up the qmake and clang3.2 on mac.

    I donwload the binary of clang3.2 from here
    "clang binary":http://llvm.org/releases/download.html

    The clang3.2 do not support a lot of c++11 features.
    like lambda, unique_ptr, std::function, bind, rvalue reference and
    so on.But according to the site "c++11 compiler progress":http://wiki.apache.org/stdcxx/C++0xCompilerSupport

    The clang3.2 should support a lot of c++11 features
    What do I lack?



  • Never success in the first time when related to environment setting
    They always pop out a lot of problems.

    my .pro file
    @
    #-------------------------------------------------

    Project created by QtCreator 2013-02-06T07:23:32

    #-------------------------------------------------

    QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = test00
    TEMPLATE = app

    CONFIG += -std=gnu++11

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui

    @

    The source codes
    @
    #include <functional>
    #include <iostream>
    #include <memory>

    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    auto A = 444; //generate warning, 
    std::cout<<A<<std::endl;
    std::unique_ptr<int> B; //generate error messages
    
    return a.exec(&#41;;
    

    }

    @

    warning : auto' type specifier is a C++11 extension [-Wc++11-extensions]

    error messages :
    error: expected '(' for function-style cast or type construction
    std::unique_ptr<int> B



  • in your qmake .pro file, add

    Qt5:
    @CONFIG += c++11@

    or Qt4:
    @CXXFLAGS += -std=c++11@

    See also http://woboq.com/blog/cpp11-in-qt5.html



  • I tried it, both
    CXXFLAGS += -std=c++11
    CONFIG += -std=c++11
    or
    CONFIG += -std=gnu++11

    I also execute the clang++ directly

    clang++ -stdlib=libstdc++ -std=c++11 main.cpp

    @
    //
    // main.cpp
    // yyyy
    //
    // Created by yyyy on 2/6/13.
    // Copyright (c) 2013 yyyy. All rights reserved.
    //

    #include <functional>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <vector>

    int main(int argc, const char * argv[])
    {

    // insert code here...
    std::cout << "Hello, World!\n";
    std::unique_ptr<int> A(new int(3)); //generate errors on this line
    

    //but this is fine
    auto func = { std::cout << "hahaha\n"; };
    func();

    return 0;
    

    }
    @

    error messages

    main.cpp:20:10: error: no member named 'unique_ptr' in namespace 'std'
    std::unique_ptr<int> A(new int(3));
    ~~~~~^
    main.cpp:20:24: error: expected '(' for function-style cast or type construction
    std::unique_ptr<int> A(new int(3));
    ~~~^
    main.cpp:20:26: error: use of undeclared identifier 'A'
    std::unique_ptr<int> A(new int(3));



  • What is the compiler command?

    Also, unique_ptr is part of the standard library. Are you using the stdlib from gcc or the libcpp?
    If you are using the one from gcc, you need to have a recent version of GCC.

    If you are on mac, it is better to use libcpp, then you need to use -stdlib=libc++
    But I think is not binary compatible with GCC's library.



  • bq. What is the compiler command?

    clang++ -stdlib=libstdc++ -std=c++11 main.cpp -o test

    Now I get rid of those errors by using the default compiler on OS X 10.8.1
    I downloaded the command line tools by XCODE

    but when I compile it by QtCreator
    It can't work

    .pro
    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    CONFIG += -std=c++11

    SOURCES += main.cpp

    @

    @
    #include <iostream>

    using namespace std;

    int main()
    {
    cout << "Hello World2!" << endl;

    auto func = [](){cout << "wahaha"<<endl; };
    
    return 0;
    

    }
    @

    the compiler of the QtCreator refer to is usr/bin/clang++

    I don't know this is the same compiler of the command line refer to or not?
    How could I check the path of the "clang++" of the terminal refer to?



  • [quote author="stereomatching" date="1360195754"]How could I check the path of the "clang++" of the terminal refer to?[/quote]
    Try typing that in the terminal:
    @which clang++@



  • [quote author="alexisdm" date="1360197491"][quote author="stereomatching" date="1360195754"]How could I check the path of the "clang++" of the terminal refer to?[/quote]
    Try typing that in the terminal:
    @which clang++@

    [/quote]
    Thanks, it is same as the QtCreator refer to
    /usr/bin/clang++

    So the problem is how could I make that QtCreator
    know I need c++11 support just like the command line do?

    command input from the terminal
    clang++ -stdlib=libstdc++ -std=c++11

    the codes with c++11 library also pass if it is compile by command line
    @
    //
    // main.cpp
    // yyyy
    //
    // Created by yyyy on 2/6/13.
    // Copyright (c) 2013 yyyy. All rights reserved.
    //

    #include <functional>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <vector>

    template<typename T>
    void print_comma_separated_list(T value)
    {
    std::cout<<value<<std::endl;
    }

    template<typename First,typename ... Rest>
    void print_comma_separated_list(First first,Rest ... rest)
    {
    std::cout<<first<<",";
    print_comma_separated_list(rest...);
    }

    int main(int argc, const char * argv[])
    {

    // insert code here...
    std::cout << "Hello, World!\n";
    
    auto func = [](){ std::cout << "hahaha\n"; };
    func();
    
    std::vector<std::string> strs{"yahoo", "haha"};
    for(auto const &data : strs){
        std::cout<<data<<std::endl;
    }
    
    std::vector<std::string> strs2 = std::move(strs);
    for(auto const &data : strs2){
        std::cout<<data<<std::endl;
    }
    
    std::unique_ptr<int> A(new int(3));
    std::cout<<*A<<std::endl;
    
    print_comma_separated_list(32, "444", 34.564564, "lalamimilolipo");
    
    return 0;
    

    }
    @



  • You should add:
    @QMAKE_CXXFLAGS += -std=c++11@

    The previously given variable, CXXFLAGS, doesn't do anything.



  • Thanks, QMAKE_CXXFLAGS += -std=c++11 did make c++11 features pass

    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt

    SOURCES += main.cpp

    QMAKE_CXXFLAGS += -std=c++11
    @

    But it can't compile when I use something related to the library
    like unique_ptr or initialise_list

    Under command line, stdlib=libstdc++ should play the trick
    How about QtCreator?Even it pass, could it work with Qt?



  • You can add "-stdlib=libstdc++" to QMAKE_CXXFLAGS too.



  • bq. You can add “-stdlib=libstdc++” to QMAKE_CXXFLAGS too.

    I tried it, but can't work

    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt

    SOURCES += main.cpp

    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -stdlib=libstdc++
    @

    part of the error messages
    @
    In file included from /usr/include/c++/4.2.1/ostream:44:
    In file included from /usr/include/c++/4.2.1/ios:47:
    In file included from /usr/include/c++/4.2.1/bits/ios_base.h:46:
    In file included from /usr/include/c++/4.2.1/bits/locale_classes.h:46:
    In file included from /usr/include/c++/4.2.1/string:47:
    In file included from /usr/include/c++/4.2.1/memory:54:
    @

    compiler do not connect to libstdc++?



  • combinations of the .pro file

    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt

    SOURCES += main.cpp

    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -stdlib=libstdc++
    @

    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    CONFIG += -stdlib=libstdc++

    SOURCES += main.cpp

    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -stdlib=libstdc++
    @

    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    CONFIG += -stdlib=libstdc++

    SOURCES += main.cpp

    QMAKE_CXXFLAGS += -std=c++11
    #QMAKE_CXXFLAGS += -stdlib=libstdc++
    @

    All of them can't work



  • The make file generated by this .pro

    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt

    SOURCES += main.cpp

    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -stdlib=libstdc++
    @

    "makefile":http://pastebin.com/2E6xMRL6

    The flags -stdlib=libstdc++ and -std=c++11
    are added into the Compiler, tools and options part
    CXXFLAGS = -pipe -std=c++11 -stdlib=libstdc++ -O2 -arch x86_64 -Wall -W $(DEFINES)

    what is the problem of the makefile generated by qmake?



  • Sorry, I did a mistake, the command should be
    -stdlib=libc++ but not -stdlib=libstdc++

    The correct command line :
    clang++ -stdlib=libc++ -std=c++11 main.cpp -o test

    command line work fine, but QtCreator still generate error message

    QtCreator setting :
    "setting":http://www.flickr.com/photos/92283971@N04/8453188038/in/photostream

    .pro file after change

    @TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    CONFIG += -stdlib=libc++

    SOURCES += main.cpp

    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -stdlib=libc++@

    error message

    clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later)
    make: *** [main.o] Error 1

    But my OS number is 10.8.1



  • The version is taken from the "QMAKE_MACOSX_DEPLOYMENT_TARGET":http://qt-project.org/doc/qt-4.8/qmake-variable-reference.html#qmake-macosx-deployment-target variable, so if it wouldn't work with that version, you get an error.



  • @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    CONFIG += -stdlib=libc++

    SOURCES += main.cpp

    #QMAKE_LFLAGS += -nodefaultlibs /usr/lib/libstdc++.dylib /usr/lib/libgcc_s.1.dylib

    QMAKE_CXXFLAGS += -stdlib=libc++
    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
    #QMAKE_CXXFLAGS += -mmacosx-version-min=10.7 #this line also give the same error message
    @

    error message
    clang: error: linker command failed with exit code 1 (use -v to see invocation)





  • [quote author="alexisdm" date="1360245040"]Try this: http://qt-project.org/forums/viewthread/17150[/quote]

    According to the informations of the link, I add
    QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
    just as the previous post

    But I get another error message
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    The contents of the makefile
    "makefile":http://pastebin.com/CaiFdNtF

    Apparently, it do not link to the proper library
    Where is the proper library?



  • According to the generated makefile, the target is still 10.4.
    Can you show the compilation output to see how clang++ is invoked ?

    If you have to add -stdlib=libc++, that means the current Qt installation uses libstdc++, and you won't be able to compile anything with both Qt and libc++, unless you recompile Qt.



  • Sorry, looks like I posted a wrong makefile
    This time I'm sure the target is 10.7
    "makefile":http://pastebin.com/SqG8hyPZ

    .pro
    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    #CONFIG += -stdlib=libc++

    SOURCES += main.cpp

    #QMAKE_LFLAGS += -nodefaultlibs /usr/lib/libstdc++.dylib /usr/lib/libgcc_s.1.dylib

    QMAKE_CXXFLAGS += -stdlib=libc++
    QMAKE_CXXFLAGS += -std=c++11
    #QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
    QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7

    #LIBS += /usr/lib/libstdc++.dylib /usr/lib/libgcc_s.1.dylib

    @

    "error messages":http://pastebin.com/0RahN86i

    bq. If you have to add -stdlib=libc++, that means the current Qt installation uses libstdc++, and you won’t be able to compile anything with both Qt and libc++, unless you recompile Qt.

    How about Qt5?Do I need to add -stdlib=libc++ if it is Qt5?
    I would upgrade my projects to Qt5 if I could skip the pain of recompile

    Whatever, now the problem is how to make QtCreator could work with clang++
    and c++11.Command line can make the job done but the makefile generated
    by qmake can't compile.



  • I give Qt5.0.1 a try, it has a same problem too
    But this is expectable since the codes have nothing
    to do with Qt but QtCreator and clang++.

    I install Qt5.0.1 and Qt4.8.4 on mac together
    Not sure this could be a problem or not

    .pro of Qt5
    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    CONFIG += c++11
    #CONFIG += -stdlib=libc++

    SOURCES += main.cpp

    #QMAKE_LFLAGS += -nodefaultlibs /usr/lib/libstdc++.dylib /usr/lib/libgcc_s.1.dylib

    QMAKE_CXXFLAGS += -stdlib=libc++
    #QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
    #QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7

    #LIBS += /usr/lib/libstdc++.dylib /usr/lib/libgcc_s.1.dylib
    @

    "makefile":http://pastebin.com/76PUzw2u

    "error messages":http://pastebin.com/WTWrwm8d



  • I am thinking of using gcc4.7 to replace clang3.2
    Compile with gcc come from the osx is fine with Qt4.8.4

    But Qt5.0.1 would generate a warning message no matter it is
    clang or gcc

    ld: warning: directory not found for option '-F/Users/yyyy/Qt5.0.1/5.0.1/clang_64/qtbase/lib'



  • I download gcc4.8 by macport

    //install gcc48
    sudo port install gcc48

    //set gcc48 as the default compiler
    sudo port select --set gcc mp-gcc48

    set the compiler under /opt/local/bin/g++ for Qt5.0.1

    .pro file

    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt

    SOURCES += main.cpp

    QMAKE_CXXFLAGS += -std=c++0x
    @

    codes
    @
    //
    // main.cpp
    // yyyy
    //
    // Created by yyyy on 2/6/13.
    // Copyright (c) 2013 yyyy. All rights reserved.
    //

    #include <functional>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <vector>

    template<typename T>
    inline void print_comma_separated_list(T value)
    {
    std::cout<<value<<std::endl;
    }

    template<typename First,typename ... Rest>
    void print_comma_separated_list(First first,Rest ... rest)
    {
    std::cout<<first<<",";
    print_comma_separated_list(rest...);
    }

    constexpr int multiply_two(int a)
    {
    return a * 2;
    }

    int main()
    {

    // insert code here...
    std::cout << "Hello, World!"<<std::endl;
    
    auto func = [](){ std::cout << "hahaha\n"; };
    func();
    
    std::vector<std::string> strs{"yahoo", "haha"};
    for(auto const &data : strs){
        std::cout<<data<<std::endl;
    }
    
    std::vector<std::string> strs2 = std::move(strs);
    for(auto const &data : strs2){
        std::cout<<data<<std::endl;
    }
    
    std::unique_ptr<int> A(new int(3));
    std::cout<<*A<<std::endl;
    
    print_comma_separated_list(32, "444", 34.564564, "lalamimilolipo");
    
    return 0;
    

    }
    @

    without any warning or errors
    but it will pop out warning if I use Qt5

    ld: warning: directory not found for option ‘-F/Users/yyyy/Qt5.0.1/5.0.1/clang_64/qtbase/lib’



  • With clang, the linker doesn't get the -stdlib=libc++ option, so it was trying to link with libstdc++:
    @clang++ -headerpad_max_install_names -mmacosx-version-min=10.6 -o test01 main.o @

    Try adding:
    @LIBS += -stdlib=libc++ -std=c++11@

    I'm not sure if -std=c++11 is needed for the linker.



  • [quote author="alexisdm" date="1360328228"]With clang, the linker doesn't get the -stdlib=libc++ option, so it was trying to link with libstdc++:
    @clang++ -headerpad_max_install_names -mmacosx-version-min=10.6 -o test01 main.o @

    Try adding:
    @LIBS += -stdlib=libc++ -std=c++11@

    I'm not sure if -std=c++11 is needed for the linker.[/quote]

    Thanks, the problem finally solved(half)

    The .pro file
    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    #CONFIG += c++11

    SOURCES += main.cpp

    LIBS += -stdlib=libc++ #don't need -std=c++11

    QMAKE_CXXFLAGS += -stdlib=libc++
    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
    @

    Not done yet, we have to change the parameters of the makefile too

    @
    ####### Compiler, tools and options

    CC = clang
    CXX = clang++
    DEFINES =
    CFLAGS = -pipe -mmacosx-version-min=10.7 -O2 -Wall -W -fPIE $(DEFINES)
    CXXFLAGS = -pipe -mmacosx-version-min=10.7 -stdlib=libc++ -std=c++11 -mmacosx-version-min=10.7 -O2 -Wall -W -fPIE $(DEFINES)
    INCPATH = -I../../../yyyy/Qt5.0.1/5.0.1/clang_64/mkspecs/macx-clang -I.
    LINK = clang++
    LFLAGS = -headerpad_max_install_names -mmacosx-version-min=10.7
    LIBS = $(SUBLIBS) -stdlib=libc++
    AR = ar cq
    RANLIB = ranlib -s
    QMAKE = /Users/yyyy/Qt5.0.1/5.0.1/clang_64/bin/qmake
    @

    change 10.6 to 10.7 since
    QMAKE_CXXFLAGS += -mmacosx-version-min=10.7 or
    QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
    would not change the mmacosx-version-min to 10.7

    Thanks for your big helps, If I am familiar with makefile
    This problem could be solved faster

    But it would generate the warning message
    ld: warning: directory not found for option '-F/Users/yyyy/Qt5.0.1/5.0.1/clang_64/qtbase/lib'

    After I go into the path and type ls -al, I really don't have anything called /qtbase/lib



  • Do some change on .pro file

    @
    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt

    SOURCES += main.cpp

    LIBS += -stdlib=libc++

    QMAKE_CXXFLAGS += -stdlib=libc++
    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
    QMAKE_LFLAGS += -mmacosx-version-min=10.7
    @

    Don't need to alter the makefile by hand

    The warning still exist, do I need to solve the warning message?
    Or I could simply ignore it?

    Oh man, I hope that the support of c++11 could become mature ASAP
    then all of us don't need to go through so many trouble
    when we want to use it.



  • According to the sites
    "Qt bug":https://bugreports.qt-project.org/browse/QTBUG-28336

    This warning could be ignored, let us hope that Qt5.1 will fixe this bug


Log in to reply