如何让clang3.2支援c++11?

stereoMatching 2013-02-06 10:57:57
刚在mac mini上安装Qt4.8.4
qmake和clang3.2好不容易装上去后
却发现clang3.2不支援C++11

clang3.2的binary
http://llvm.org/releases/download.html
下载之后直接解压缩

.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


程式

#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; //警告,
std::cout<<A<<std::endl;
std::unique_ptr<int> B; //错误讯息

return a.exec();
}


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

错误讯息 :
error: expected '(' for function-style cast or type construction
std::unique_ptr<int> B

我在QtCreator设置的编译器,该执行档的名称是"clang"
...全文
1375 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
谢谢,帮你顶一下。
stereoMatching 2013-02-08
  • 打赏
  • 举报
回复
几番折腾以后,我升级成Qt5.0.1并舍弃clang3.2而改用gcc4.8。 gcc4.8在mac下的安装方法 http://www.ficksworkshop.com/blog/14-coding/65-installing-gcc-on-mac .pro

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

SOURCES += main.cpp

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

#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;
}
透过QtCreator编译,一切正常 但是若使用了跟Qt有关的部分 会跑出warning ld: warning: directory not found for option '-F/Users/yyyy/Qt5.0.1/5.0.1/clang_64/qtbase/lib'
stereoMatching 2013-02-07
  • 打赏
  • 举报
回复
我应该link到那个dylib才对?正确的路径是?
stereoMatching 2013-02-07
  • 打赏
  • 举报
回复
做了一点改进

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

SOURCES += main.cpp

QMAKE_CXXFLAGS += -stdlib=libc++
QMAKE_CXXFLAGS += -std=c++11
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7 #1
不加#1的话,会跑出 invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later) 这条错误讯息,但是加上去以后则会出现 clang: error: linker command failed with exit code 1 (use -v to see invocation) 这条错误讯息 连接错误,但是透过commend line编译就link得很顺利 makefile的内容 http://pastebin.com/CaiFdNtF 看不出所以然 不会要我去检查mkspec的内容吧
乔巴好萌 2013-02-07
  • 打赏
  • 举报
回复
试试qmake,make看看编译期的参数和你设置的是否一致
引用 8 楼 stereoMatching 的回复:
我没有升级gcc,改用system path下的clang++编译(用XCODE下载了command line tools) 结果居然编译过关了 指令 clang++ -stdlib=libc++ -std=c++11 main.cpp -o test 代码 CSS code ? 12345678910111213141516171819202122232……
乔巴好萌 2013-02-07
  • 打赏
  • 举报
回复
惭愧,平时最多也就是用auto-complete-clang做 sublime text插件写写代码 真正项目中用clang编译的不多
引用 7 楼 allencui0313 的回复:
听 江浩同学说过,clang这个东西编译调试很快,楼上各位若有心得写个总结,pm我,帮你们置顶哈。
stereoMatching 2013-02-07
  • 打赏
  • 举报
回复
我没有升级gcc,改用system path下的clang++编译(用XCODE下载了command line tools) 结果居然编译过关了 指令 clang++ -stdlib=libc++ -std=c++11 main.cpp -o test 代码

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;
}
.pro file

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += -std=c++11
 
SOURCES += main.cpp
输入 which clang++ 的结果是 /usr/bin/clang++ 这跟我QtCreator设定的compiler一致,但是用QtCreator编译却一直不过关 难道是QtCreator的设定错误了吗?
  • 打赏
  • 举报
回复
听 江浩同学说过,clang这个东西编译调试很快,楼上各位若有心得写个总结,pm我,帮你们置顶哈。
乔巴好萌 2013-02-06
  • 打赏
  • 举报
回复
mac os里面默认自带的gcc版本较低 建议还是升级下 参见下面这篇文章 http://stackoverflow.com/questions/13958197/c11-on-mac-with-clang-or-gcc 和 http://stackoverflow.com/questions/13351032/clangs-support-of-c-11-lambda/13364165#13364165
乔巴好萌 2013-02-06
  • 打赏
  • 举报
回复
按照方式 clang++ -std=c++11 -I/usr/include/c++/4.6.1 -I/usr/lib/clang/2.9/include test.cpp -o test 是编译没问题的 如果用命令行编译可也看出 clang编译时是依赖include目录的 所以最好自己配置下include默认 或者设置环境变量
乔巴好萌 2013-02-06
  • 打赏
  • 举报
回复
刚才又看了下 clang应该对c++ 支持的跟进还是很快的 而且应该是2.9版本的就开始支持 官网上说要加-std=c++11 或 -std=gnu++11选项的 我用g++ 4.7.1 编译通过 没问题 我直接用的clang 3.1编译没通过 我用命令行编译的 clang -std=c++11 test.cpp In file included from test.cpp:1: In file included from /usr/lib/gcc/i686-linux-gnu/4.4/../../../../include/c++/4.4/functional:55: In file included from /usr/lib/gcc/i686-linux-gnu/4.4/../../../../include/c++/4.4/typeinfo:34: In file included from /usr/lib/gcc/i686-linux-gnu/4.4/../../../../include/c++/4.4/exception:148: /usr/lib/gcc/i686-linux-gnu/4.4/../../../../include/c++/4.4/exception_ptr.h:143:13: error: unknown type name 'type_info' const type_info* ^ In file included from test.cpp:1: In file included from /usr/lib/gcc/i686-linux-gnu/4.4/../../../../include/c++/4.4/functional:57: /usr/lib/gcc/i686-linux-gnu/4.4/../../../../include/c++/4.4/tuple:323:15: error: no matching function for call to 'forward' : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { } 可也看到引用的头文件是4.4的gcc的头文件 于是换了一个默认发行版自带就是4.6的g++ 按照以下方式编译了下 clang++ -std=c++11 -I/usr/include/c++/4.6.1 -I/usr/lib/clang/2.9/include test.cpp -o test 第2个include是用于定义stdarg.h方式的
stereoMatching 2013-02-06
  • 打赏
  • 举报
回复
引用 2 楼 cnsword 的回复:
Clang 3.2 supports most of the language features added in the latest ISO C++ standard,C++ 2011. Use -std=c++11 or -std=gnu++11 to enable support for these features. 需要添加编译选项 -std=c++11 或……
我两个都试过了,可是都不行,不知道为什么 加在.pro上
开发者说 2013-02-06
  • 打赏
  • 举报
回复
Clang 3.2 supports most of the language features added in the latest ISO C++ standard,C++ 2011. Use -std=c++11 or -std=gnu++11 to enable support for these features. 需要添加编译选项 -std=c++11 或 -std=gnu++11
乔巴好萌 2013-02-06
  • 打赏
  • 举报
回复
这个貌似编译器的跟进速度比标准是有那么一点点偏差。。。 还是考虑上gcc吧

16,814

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧