Compile std::regex_iterator with gcc

2019-02-16 21:06发布

问题:

I can create .o file with g++ -c test.cpp -std=c++0x, but cant link it, got next errors:

test.cpp:(.text+0xe5): undefined reference to `std::regex_iterator<char const*, char, std::regex_traits<char> >::regex_iterator(char const*, char const*, std::basic_regex<char, std::regex_traits<char> > const&, std::bitset<11u>)'
test.cpp:(.text+0xf1): undefined reference to `std::regex_iterator<char const*, char, std::regex_traits<char> >::regex_iterator()'

Code:

#include <regex> 
#include <iostream> 

#include <string.h>

typedef std::regex_iterator<const char *> Myiter; 
int main() 
{ 
    const char *pat = "axayaz"; 
    Myiter::regex_type rx("a"); 
    Myiter next(pat, pat + strlen(pat), rx); 
    Myiter end; 


    return (0); 
} 

回答1:

The GNU C++ standard library supports <regex>, but not until version 4.9.0. (The headers were present in earlier versions, but were unusable.)

The other compilers don't support it, as far as I can see.

You can use a different library if you use an older GCC.