当我从 Xcode4.6 运行以下代码片段时,它编译并运行良好。但是当我尝试使用命令行工具 (clang++) 编译它时,它失败了。
#include <iostream>
#include <memory>
int main(int argc, const char * argv[])
{
std::unique_ptr<int> foo(new int(0));
// insert code here...
std::cout << "Hello, this is cool giri World!\n";
return 0;
}
编译日志如下:
$ clang --version Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn) Target: x86_64-apple-darwin12.2.0 Thread model: posix $ clang++ main.cpp -stdlib=libc++ -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ -I /usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include/ main.cpp:7:10: error: no member named 'unique_ptr' in namespace 'std' std::unique_ptr foo(new int(0)); ~~~~~^ main.cpp:7:24: error: expected '(' for function-style cast or type construction std::unique_ptr foo(new int(0)); ~~~^ main.cpp:7:26: error: use of undeclared identifier 'foo' std::unique_ptr foo(new int(0)); ^ 3 errors generated.
最佳答案
尝试使用 clang 自己的标准库:
clang -std=c++11 -stdlib=libc++ main.cpp
默认是 GNU 的标准库 (libstdc++
),但 Apple 包含的版本很旧,不支持 C++11。
关于c++ - 在 xcode 4.6 中构建但使用命令行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829166/