如何使用TR1在Mac OS上编译C ++

Mic*_*ier 1 c++ macos xcode tr1

我有一个基于Linux的现有产品,并且正在尝试将其移植到Mac OS。

msoulier@merlin:~$ xcode-select -v
xcode-select version 2343.
msoulier@merlin:~$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Run Code Online (Sandbox Code Playgroud)

问题在于它使用了tr1 / tuple库,由于某种原因,它不在默认的include路径中。

msoulier@merlin:~$ ls /usr/include/c++/4.2.1/tr1/tuple
/usr/include/c++/4.2.1/tr1/tuple
Run Code Online (Sandbox Code Playgroud)

这样就可以了,应该在基于上述--with-gxx-include-dir选项的include路径中,

但是

msoulier@merlin:~$ cat hello.cpp
#include <iostream>
#include <tr1/tuple>

using namespace std;

int main(void) {
    cout << "Hello, World!" << endl;
    return 0;
}
msoulier@merlin:~$ g++ -o hello hello.cpp
hello.cpp:2:10: fatal error: 'tr1/tuple' file not found
#include <tr1/tuple>
        ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

为什么这不行呢?

谢谢。

Bre*_*nks 6

简短的答案:用调用clang ++ -stdlib=libstdc++tr1标题就在那里。

长答案: 错误的原因以及包括两组C ++的原因是macOS / Xcode具有两个可以构建的不同C ++标准库:一个旧的GNU libstdc++和一个新的和现代的LLVM libc++

随着MacOS的10.12塞拉利昂的,默认的是现在libc++libstdc++已经过时了。libstdc++v4.2.1相当老,并且早于C ++ 11(因此tr1标头)。如果您要长期维护此代码,则值得花时间使其符合C ++ 11(例如#include <tuple>

更新: Xcode 10不再允许针对构建libstdc++。更新您的代码库以使用标准的C ++ 11标头,或者如果确实没有选择,请使用Xcode 9。