amos
amos

Reputation: 5402

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

Why doesn't this work:

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

Compiled as:

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

If I allow stdlib to be libc++ then it compiles. Regex is c++11, but clang doesn't seem to have a problem with both -std=c++11 -stdlib=libstdc++ per se. On my machine at least, it looks like there's something I could use in /usr/include/regex.h, but that's not standard, and besides there are things other than regex I'd like to use (e.g. std::to_string).

The reason this has come up is because I'd like to link to a 3rd party library (for which I don't have the source) which is complied as std::string and not std::__1::basic_string, but my code uses std::regex and std::to_string. I'm not sure I want to introduce a dependency on boost.

Upvotes: 5

Views: 2741

Answers (2)

Jeremy Huddleston Sequoia
Jeremy Huddleston Sequoia

Reputation: 23623

The version of libstdc++ shipped with OS X comes from gcc-4.2.1, the last version of GCC before FSF decided to adopt GPL3. Apple deprecated libstdc++ in OS X Lion in favor of libc++ from the LLVM Project. If you want to use C++11 on OS X, you should be using -std=c++11 -stdlib=libc++

Upvotes: 2

Miles Budnek
Miles Budnek

Reputation: 30569

Apple ships a very old version of libstdc++ with OS X (4.2.1 I think). That version did not fully support C++11, so you'll need to get a newer version to use std::regex.

Upvotes: 5

Related Questions