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

2

5

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.

Euton answered 11/1, 2016 at 19:21 Comment(8)
Maybe your libstc++ isn't C++11 compliant?Bayou
I'm OSX 10.10.5, so I'd think Apple would be on task...Euton
This might help.Aft
<regex> was not properly functional in libstdc++ until the version that shipped with gcc-4.9, see this. Maybe you have an older version.Exurbia
clang has it's own stdlib, may be you can use it?Unpleasantness
@Unpleasantness can you clarify? I'd guess that's what it's already using.Euton
I believe, what you are using is a gnu standard library. Clang has it's own, libc++.Unpleasantness
@Unpleasantness right, but libc++ is not binary compatible with the 3rd party library I want to use.Euton
S
5

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.

Sly answered 11/1, 2016 at 19:48 Comment(3)
If -std=c++11 -stdlib=libstdc++ are mutually exclusive, IMHO, there should a warning!Euton
They aren't technically mutually exclusive, even with the version shipped by default. libstdc++ 4.2.1 does support some C++11 features that had been proposed at the time it released (mid 2007).Sly
I suppose this SO question can serve as the warning! :-)Euton
B
2

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++

Borges answered 12/1, 2016 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.