Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ can't find regex even with -std=c++11 macOSX

So I'm trying to use the c++ 2011 regex at home on my Mac. I'm coding using Eclipse, with a little dabbling with compiling from the terminal.

This is what happens when I try to compile my code:

$ c++ -std=c++11 -o a *.cpp
scanner.cpp:11:10: fatal error: 'regex' file not found
#include <regex>  // to use this need to use -std=c++11 flag in compiler
         ^
1 error generated.

This happens if I use the -std=c++11 flag or not.

Here's my c++ -v:

$ c++ -v
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix

Now I've found on my computer what looks to be the file I want to include in my project at "usr/lib/c++/v1/regex". But that's not in the include files in eclipse, and I have no idea how to make my compiler find it.

How can I add this to the include paths, if that is what I need to do to make this work?

Update:

tried

$ c++ -I/usr/lib/c++/v1 -o a *.cpp

which results in the error

Undefined symbols for architecture x86_64:

followed what appears to be a reference to every line in my code, such as

  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::compare(char const*) const", referenced from:
      std::__1::basic_regex<char, std::__1::regex_traits<char> >::__start_matching_list(bool) in scanner-LSmxM4.o
like image 458
wolf123450 Avatar asked Sep 07 '13 04:09

wolf123450


1 Answers

Add -stdlib=libc++ in addition to -std=c++11. libc++ is the standard library implementation that supports C++11. By default, it uses libstdc++ which does not support C++11. The whole command should look like:

c++ -std=c++11 -stdlib=libc++ -o output source.cpp
like image 176
Cornstalks Avatar answered Oct 21 '22 00:10

Cornstalks