c++ - Why doesn't this program run properly under Clang or GCC? -
i'm trying run cppreference's regex_search example:
#include <iostream> #include <string> #include <regex> int main() { std::string lines[] = {"roses #ff0000", "violets #0000ff", "all of base belong you"}; std::regex color_regex("#([a-f0-9]{2})" "([a-f0-9]{2})" "([a-f0-9]{2})"); (const auto &line : lines) { std::cout << line << ": " << std::regex_search(line, color_regex) << '\n'; } std::smatch color_match; (const auto &line : lines) { std::regex_search(line, color_match, color_regex); std::cout << "matches '" << line << "'\n"; (size_t = 0; < color_match.size(); ++i) std::cout << << ": " << color_match[i] << '\n'; } }
this program compiles clang++ 3.4-1ubuntu3 , gcc 4.8.2, running gives error:
terminate called after throwing instance of 'std::regex_error' what(): regex_error aborted (core dumped)
this question indicated problem 1 gcc, compiling clang produces same problem. explicitly passing in library @ each step command:
clang++ -stdlib=libstdc++ -std=c++11 -h -c regex.cpp -o object.o && clang++ -stdlib=libstdc++ object.o -o regex
causes error @ runtime, despite fact last lines of first command's output are:
.. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_constants.h .. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_error.h .. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_cursor.h .. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_nfa.h ... /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_nfa.tcc .... /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/regex .. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_compiler.h .. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_grep_matcher.h ... /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex_grep_matcher.tcc .... /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/regex .. /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/regex.h
...which indicate appropriate headers present on system.
you still use incomplete gcc
standard library implementation, explicitly tell clang
use -stdlib=libstdc++
.
the standard library implementation comes clang
libc++
, need -stdlib=libc++
instead.
Comments
Post a Comment