[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.htmlThe 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++0xCompilerSupportThe 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 = appCONFIG += -std=gnu++11
SOURCES += main.cpp
mainwindow.cppHEADERS += 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();
}
@
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@ -
I tried it, both
CXXFLAGS += -std=c++11
CONFIG += -std=c++11
or
CONFIG += -std=gnu++11I 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 XCODEbut when I compile it by QtCreator
It can't work.pro
@
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += -std=c++11SOURCES += 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="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++11the 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;
}
@ -
Thanks, QMAKE_CXXFLAGS += -std=c++11 did make c++11 features pass
@
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qtSOURCES += 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_listUnder command line, stdlib=libstdc++ should play the trick
How about QtCreator?Even it pass, could it work with Qt? -
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 -= qtSOURCES += 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 -= qtSOURCES += 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 -= qtSOURCES += 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 testcommand 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 1But 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 postBut 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/CaiFdNtFApparently, 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.