MinGW上使用正则表达式

C++使用正则表达式一文中,曾提到过MinGW中使用正则的一些问题。可是今天再试却死活不成功了。报了一大串错误:

\AppData\Local\Temp\ccxSTtRm.o:regex_test.cpp:(.text$_ZNSt11basic_regexIcSt12regex_traitsIcEEC1EPKcj[std::basic_regex<char, std::regex_traits<char> >::basic_regex(char const*, unsigned int)]+0x60): undefined reference to `std::basic_regex<char, std::regex_traits<char> >::_M_compile()'
\AppData\Local\Temp\ccxSTtRm.o:regex_test.cpp:(.text$_ZSt11regex_matchIPKccSt12regex_traitsIcEEbT_S4_RKSt11basic_regexIT0_T1_ESt6bitsetILj11EE[bool std::regex_match<char const*, char, std::regex_traits<char> >(char const*, char const*, std::basic_regex<char, std::regex_traits<char> > const&, std::bitset<11u>)]+0x52): undefined reference to `bool std::regex_match<char const*, std::allocator<std::sub_match<char const*> >, char, std::regex_traits<char> >(char const*, char const*, std::match_results<char const*, std::allocator<std::sub_match<char const*> > >&, std::basic_regex<char, std::regex_traits<char> > const&, std::bitset<11u>)'
collect2: ld returned 1 exit status

搜索了一下,在老外的一个网站上,有人说是gcc版本的问题:

On 2012-07-26 16:46, Daniel Colascione wrote:
> $ g++ -std=gnu++0x foo.cpp
> /tmp/ccS3vCW7.o:foo.cpp:(.text$_ZNSt11basic_regexIcSt12regex_traitsIcEEC1EPKcj[std::basic_regex<char,
> std::regex_traits<char> >::basic_regex(char const*, unsigned
> int)]+0x60): undefined reference to `std::basic_regex<char,
> std::regex_traits<char> >::_M_compile()'
> collect2: ld returned 1 exit status

GCC 4.5 does not have full support for C++0x.  We'll need an upgrade to 
4.7 for this to work.

Yaakov

于是升级gcc

$ mingw-get upgrade gcc
mingw-get.exe: *** ERROR *** upgrade gcc: package is not installed
mingw-get.exe: *** ERROR *** upgrade gcc: package is not installed
http://prdownloads.sourceforge.net/mingw/gcc-4.7.2-1-mingw32-lic.tar.lzma?download
20.72 kB / 20.72 kB     |================================================| 100%
http://prdownloads.sourceforge.net/mingw/mingwrt-3.20-2-mingw32-dll.tar.lzma?download
5.57 kB / 5.57 kB       |================================================| 100%
http://prdownloads.sourceforge.net/mingw/libpthreadgc-2.9.0-mingw32-pre-20110507-2-dll-2.tar.lzma?download
26.71 kB / 26.71 kB     |================================================| 100%
http://prdownloads.sourceforge.net/mingw/libintl-0.18.1.1-2-mingw32-dll-8.tar.lzma?download
44.14 kB / 44.14 kB     |================================================| 100%
http://prdownloads.sourceforge.net/mingw/binutils-2.23.1-1-mingw32-bin.tar.lzma?download
3.83 MB / 3.83 MB       |================================================| 100%
http://prdownloads.sourceforge.net/mingw/pthreads-w32-2.9.0-mingw32-pre-20110507-2-dev.tar.lzma?download
21.05 kB / 21.05 kB     |================================================| 100%
http://prdownloads.sourceforge.net/mingw/libgomp-4.7.2-1-mingw32-dll-1.tar.lzma?download
20.01 kB / 20.01 kB     |================================================| 100%
http://prdownloads.sourceforge.net/mingw/libssp-4.7.2-1-mingw32-dll-0.tar.lzma?download
16.94 kB / 16.94 kB     |================================================| 100%
http://prdownloads.sourceforge.net/mingw/libiconv-1.14-2-mingw32-dll-2.tar.lzma?download
535.88 kB / 535.88 kB   |================================================| 100%
http://prdownloads.sourceforge.net/mingw/libgcc-4.7.2-1-mingw32-dll-1.tar.lzma?download
40.74 kB / 40.74 kB     |================================================| 100%
http://prdownloads.sourceforge.net/mingw/mingwrt-3.20-2-mingw32-dev.tar.lzma?download
242.96 kB / 242.96 kB   |================================================| 100%
http://prdownloads.sourceforge.net/mingw/gcc-core-4.7.2-1-mingw32-bin.tar.lzma?download
9.48 MB / 9.48 MB       |================================================| 100%

 

$ mingw-get upgrade g++
http://prdownloads.sourceforge.net/mingw/libquadmath-4.7.2-1-mingw32-dll-0.tar.lzma?download
156.45 kB / 156.45 kB   |================================================| 100%
http://prdownloads.sourceforge.net/mingw/libstdc++-4.7.2-1-mingw32-dll-6.tar.lzma?download
202.75 kB / 202.75 kB   |================================================| 100%
http://prdownloads.sourceforge.net/mingw/gcc-c++-4.7.2-1-mingw32-bin.tar.lzma?download
6.11 MB / 6.11 MB       |================================================| 100%
install: libquadmath-4.7.2-1-mingw32-dll-0.tar.lzma
 installing libquadmath-4.7.2-1-mingw32-dll-0.tar.lzma
upgrade: libstdc++-4.7.2-1-mingw32-dll-6.tar.lzma
 removing release libstdc++-4.5.2-1-mingw32-dll-6.tar.lzma
 installing libstdc++-4.7.2-1-mingw32-dll-6.tar.lzma
upgrade: gcc-c++-4.7.2-1-mingw32-bin.tar.lzma
 removing release gcc-c++-4.5.2-1-mingw32-bin.tar.lzma
 installing gcc-c++-4.7.2-1-mingw32-bin.tar.lzma

升级成功后在去编译,果然通过了:

$ g++ -std=c++0x regex_test.cpp -o regex
regex_test.cpp: In function 'int main()':
regex_test.cpp:12:19: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
regex_test.cpp:13:19: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
regex_test.cpp:14:19: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
regex_test.cpp:15:19: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

可是,程序打开就出错,改代码把那几个警告去掉后,依然不能运行。

继续看刚才那个网页,又发现这么一段话:

std::regex is not fully implemented; upgrading to GCC 4.7 won't change 
anything. there are several todo notices in regex header files hinting 
at this.

have a look at boost::regex for the time being; should be close enough 
to std::regex.

没办法,先放弃gcc了,改在vs2010里面编译。可是,vs的regex好娇贵啊,\d,\s,\w这些貌似都没用。

 

 

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注