使用gcc编译std::regex_iterator

Compile std::regex_iterator with gcc

本文关键字:regex iterator std gcc 编译 使用      更新时间:2023-10-16

我可以用g++ -c test.cpp -std=c++0x创建。o文件,但不能链接它,得到下一个错误:

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()'
代码:

#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); 
} 

GNU c++标准库支持<regex>,但直到4.9.0版本才支持。(头文件在早期版本中存在,但无法使用。)

据我所知,其他编译器不支持它。