【发布时间】:2013-04-05 13:47:50
【问题描述】:
我已经构建了自己的 libc++,我通常将它包含在 -I /path/to/lib/include -L /path/to/lib/lib 中。但现在我必须与使用 Mac 的其他人共享一个项目,我想给他们一个“正常工作”的 Makefile™。
考虑以下程序:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
uint32_t nums[100];
for (size_t i = 0; i < 10; ++i)
{
nums[i] = 666;
}
vector<int> hello{1, 2, 3, 4, 5, 6, 7, 8};
for_each(hello.begin(), hello.end(), [](int tal)
{
cout << tal << endl;
});
}
当我用clang++ -o test test.cc 编译它时,我自然会得到与缺少-std=c++11 标志有关的错误。好的,让我们添加它clang++ -std=c++11 -o test test.cc。这给出了几个错误,其中之一是
test.cc:15:17: error: no matching constructor for initialization of 'vector<int>'
vector<int> hello{1, 2, 3, 4, 5, 6, 7, 8};
好的,我需要一个支持 C++11 的 C++ 库。
clang++ -std=c++11 -stdlib=libc++ -o test test.cc
test.cc:1:10: fatal error: 'algorithm' file not found
#include <algorithm>
我对此的解决方案是使用 -I 和 -L 指向我手动编译的 libc++。
假设我要与之分享的人没有那个,但至少有 XCode。我该怎么做才能使上述代码可复制?当然,OS X 必须附带 C++11 功能???
[编辑]
事实证明,自从我使用来自 homwbrew 的 xcode 安装 llvm 后,当我执行 which clang 时,clang 就出现了。我认为自制程序中的clang不会符号链接到/usr/local/bin,但显然它确实如此。所以我想(和以前一样)吸取的教训是永远不要假设,而是要 RTFM!
【问题讨论】:
-
取决于 OSX 的版本,包含的 libstdc++ 是否支持某些 c++11。我认为即使在最新的版本中,所有功能也不是都可用。
-
安装命令行工具后,此代码可在 Mountain Lion (clang-425.0.27) 上的最新 XCode (4.6.1) 上干净编译
-
@Petesh Weird,我有 Mountain Lion,2 天前全新安装,最新的 XCode 安装了命令行工具......你传递什么样的标志来编译它?跨度>
-
@Petesh 事实证明这并不奇怪。当我以为我在使用 XCode clang 时,我正在使用 homebrew clang。应该按照接受的解决方案的建议完成
which clang。现在,使用 XCode clang,我只需要指定-std=c++11。