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

时间:2017-03-22 02:13:08

标签: c++ xcode macos 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

问题是它使用了tr1 / tuple库,出于某种原因, 这不在默认的包含路径中。

msoulier@merlin:~$ ls /usr/include/c++/4.2.1/tr1/tuple
/usr/include/c++/4.2.1/tr1/tuple

所以它就在那里,它应该在基于的包含路径中 上面的--with-gxx-include-dir选项,

然而

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.

为什么这不起作用?

感谢。

1 个答案:

答案 0 :(得分:4)

简答:使用-stdlib=libstdc++调用clang ++,tr1标题将在那里。

答案很长: 你的错误和2套C ++包含的原因是macOS / Xcode有两个不同的C ++标准库你可以构建:旧的GNU libstdc++,以及新的和现代的LLVM libc++。 / p>

自macOS 10.12 Sierra起,默认为libc++libstdc++已弃用。 libstdc++已经过时了,v4.2.1,并且早于C ++ 11(因此tr1标题)。如果您要长期维护此代码,那么值得花时间使其符合C ++ 11标准(即#include <tuple>

更新:Xcode 10不再允许针对libstdc++进行构建。更新代码库以使用标准C ++ 11标头,或者使用Xcode 9,如果这不是一个选项。

相关问题