我安装了Xcode 4.3并想测试这个C++ 11程序:
#include <type_traits>
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
但是,它找不到type_traits标题:
~ $ c++ -o test main.cpp
main.cpp:1:10: fatal error: 'type_traits' file not found
#include <type_traits>
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
看来我正在使用正确的编译器:
~ $ c++ -v
Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)
我检查了默认的包含路径:
~ $ `c++ --print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin11"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/backward
/usr/local/include
/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include
/usr/include
/System/Library/Frameworks …Run Code Online (Sandbox Code Playgroud) 这个简单的程序(在Linux上编译时)将根据是否编译而正确地给出两个不同的答案-std=c++0x.
问题:我无法在OS X(Mountain Lion,10.8 SDK)上重现同样的事情.我错过了什么?
#include <iostream>
#include <sstream>
class Thing : public std::ostringstream
{
public:
Thing() : std::ostringstream() {}
virtual ~Thing() { std::cerr << str(); }
};
int main(int argc, const char * argv[]) {
Thing() << "Hello" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
要了解我的意思,请执行以下操作(首先在Linux上,看看它应该如何工作):
> g++ main.cpp
> ./a.out
0x401471
Run Code Online (Sandbox Code Playgroud)
> g++ -std=c++0x main.cpp
> ./a.out
Hello
Run Code Online (Sandbox Code Playgroud)
第一个将打印十六进制地址,第二个将打印"Hello".这是正确的行为,因为操作符<<解析为两个不同的东西(C++ 03中没有右值引用,所以你去了).
现在,在OS X上尝试相同的事情:
> xcrun c++ main.cpp
> ./a.out
0x10840dd88
Run Code Online (Sandbox Code Playgroud)
(这正确地产生十六进制输出.)
> xcrun c++ -std=c++0x main.cpp
> …Run Code Online (Sandbox Code Playgroud) 我正在学习c ++与C++ Primer,第5版.
我正在尝试使用clang ++编译一个带有C++ 11特性的简单c ++程序,但是我得到了应该是有效代码的编译错误.
这是一个例子:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
int n = 0;
auto *p = &n; //<-- this compiles
cout << *p << endl;
vector<string> articles = {"a", "an", "the"}; //<-- this fails; copied from the book
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是完整的错误:
$ clang++ -std=c++11 -v test.cpp -o test
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-apple-darwin10.8.0
Thread model: posix
"/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-apple-macosx10.6.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level …Run Code Online (Sandbox Code Playgroud)