简体   繁体   English

如何在OSX Lion上使用clang 3.2编译C ++ 11?

[英]How to compile C++11 with clang 3.2 on OSX lion?

I am trying to compile the following C++ program which relies on the C++11 <thread> header. 我正在尝试编译以下依赖于C ++ 11 <thread>标头的C ++程序。 I am trying to do this on OSX Lion. 我想在OSX Lion上做这件事。

#include <iostream>
#include <thread>
#include <vector>

void hello()
{
    std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
}

int main()
{
    std::vector<std::thread> threads;

    for(int i = 0; i < 5; i++)
    {
        threads.push_back(std::thread(hello));
    }

    for(auto& thread: threads)
    {
        thread.join();
    }

    return 0;
}

The above program compiles fine with g++ 4.7 which I installed using the homebrew package manager. 使用自制软件包管理器安装的g ++ 4.7上面的程序编译得很好。 However when trying to compile the above program with clang 3.2 (also installed using the homebrew package manager) I get the following error message: 但是当尝试使用clang 3.2(也使用自制软件包管理器安装)编译上述程序时,我收到以下错误消息:

Zameers-MacBook-Air:tmp zmanji$ clang++ -v -std=c++11 test.cpp 
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-apple-darwin11.3.0
Thread model: posix
 "/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -v -resource-dir /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2 -fmodule-cache-path /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/clang-module-cache -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/zmanji/tmp -ferror-limit 19 -fmessage-length 101 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.7.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/test-Zkjucl.o -x c++ test.cpp
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-apple-darwin11.3.0
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64"
ignoring nonexistent directory "/usr/include/c++/4.0.0"
ignoring nonexistent directory "/usr/include/c++/4.0.0/i686-apple-darwin8/"
ignoring nonexistent directory "/usr/include/c++/4.0.0/backward"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2.1
 /usr/include/c++/4.2.1/backward
 /usr/local/include
 /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
test.cpp:2:10: fatal error: 'thread' file not found
#include <thread>
         ^
1 error generated.

It seems that clang cannot find the <thread> header but I am not sure why. 似乎clang找不到<thread>标题,但我不确定为什么。

Edit: I tried sharth's answer below and now I am getting the error that the <iostream> header does not exist. 编辑:我在下面尝试了sharth的答案,现在我收到<iostream>标头不存在的错误。

Zameers-MacBook-Air:tmp zmanji$ clang++ -v -std=c++11 -stdlib=libc++ test.cpp 
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-apple-darwin11.3.0
Thread model: posix
 "/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -v -resource-dir /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2 -fmodule-cache-path /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/clang-module-cache -stdlib=libc++ -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/zmanji/tmp -ferror-limit 19 -fmessage-length 203 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.7.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/test-k2Alf4.o -x c++ test.cpp
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-apple-darwin11.3.0
ignoring nonexistent directory "/usr/local/Cellar/llvm/3.2/bin/../lib/c++/v1"
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^
1 error generated.

您需要使用libc ++而不是libstdc ++

clang++ -std=c++11 -stdlib=libc++ foo.cc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM