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

24

我正在尝试编译以下依赖于C++11 <thread> 头文件的C++程序。我正在尝试在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;
}

我使用homebrew软件包管理器安装的g++ 4.7,可以成功编译上述程序。但是,当我尝试使用同样通过homebrew软件包管理器安装的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.

看起来clang找不到<thread>头文件,但我不确定原因。
编辑:我尝试了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.

我相信应该是 -pthread-std=c++11 - Rapptz
1个回答

47

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

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

3
你没有使用系统的clang编译器,而是使用了homebrew的clang编译器。由于某些原因,编译器正在寻找libc++库文件,它们推荐在Linux上安装在/usr/include/c++/v1目录下。但在OS X上,该库文件存储在/usr/lib/c++/v1目录下,这是系统clang编译器会查找的位置。 - Bill Lynch
6
如果您像这样调用 $ clang++ -std=c++11 -stdlib=libc++ -o test1 test1.cc -I /usr/lib/c++/v1/ -L /usr/lib/c++/v1/,则仍可以使用Homebrew的clang。 - evading
+1 救了我!我在OSX上使用Clang 3.3。奇怪为什么它不会默认启用? - Viet
从OS X 10.9开始,libc++现在是默认选项。 - Bill Lynch
1
我想知道为什么-std=c++11不意味着使用适当的-stdlib设置。至少作为可覆盖的默认值。奇怪... - MvG
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接