如何获得 -std=c++11 w/libstdc++?

How to get -std=c++11 w/ libstdc++?

为什么这不起作用:

#include <regex>
int main() {
   return 0;
}

编译为:

clang++ -std=c++11 -stdlib=libstdc++ temp.cpp
temp.cpp:1:10: fatal error: 'regex' file not found
#include <regex>
         ^
1 error generated.


clang++ --version
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

如果我允许 stdliblibc++ 则它会编译。正则表达式是 c++11,但 clang 似乎对 -std=c++11 -stdlib=libstdc++ 本身没有问题。至少在我的机器上,看起来我可以在 /usr/include/regex.h 中使用某些东西,但这不是标准的,此外还有一些我想使用的正则表达式以外的东西(例如 std::to_string)。

出现这个问题的原因是因为我想 link 到第 3 方库(我没有源代码),它被编译为 std::string 而不是std::__1::basic_string,但我的代码使用 std::regexstd::to_string。我不确定是否要引入对 boost 的依赖。

Apple 发布了带有 OS X(我认为是 4.2.1)的非常旧版本的 libstdc++。该版本不完全支持 C++11,因此您需要更新版本才能使用 std::regex.

OSX 附带的 libstdc++ 版本来自 gcc-4.2.1,这是 FSF 决定采用 GPL3 之前的最后一个 GCC 版本。 Apple 在 OS X Lion 中弃用了 libstdc++,转而使用 LLVM 项目中的 libc++。如果你想在 OS X 上使用 C++11,你应该使用 -std=c++11 -stdlib=libc++