我正在尝试在我的C ++项目中使用GNU C库正则表达式功能,特别是我正在尝试使用regex_t regcomp,regexec,regfree函数。在编译时,我得到错误,指出这些符号是未定义的:
me> g++ -I/me/myheaders/ -c RootGenerator.cpp -o RootGenerator.o -std=gnu++0x -Wall
RootGenerator.cpp: In function ‘std::string findFirstFileInCurrentDirectory(std::string)’:
RootGenerator.cpp:1072: error: ‘regex_t’ was not declared in this scope
RootGenerator.cpp:1072: error: expected ‘;’ before ‘re’
RootGenerator.cpp:1073: error: ‘re’ was not declared in this scope
RootGenerator.cpp:1073: error: ‘REG_EXTENDED’ was not declared in this scope
RootGenerator.cpp:1073: error: ‘REG_NOSUB’ was not declared in this scope
RootGenerator.cpp:1073: error: ‘regcomp’ was not declared in this scope
RootGenerator.cpp:1074: error: expected ‘;’ before ‘int’
RootGenerator.cpp:1084: error: ‘status’ was not declared in this scope
RootGenerator.cpp:1084: error: ‘regexec’ was not declared in this scope
RootGenerator.cpp:1092: error: ‘REG_NOMATCH’ was not declared in this scope
RootGenerator.cpp:1108: error: ‘regfree’ was not declared in this scope
我意识到正则表达式标头是TR1实现,因此对我的GCC版本是实验性的。我根据首次尝试编译时收到的编译器警告添加了-std=gnu++0x
编译器选项,但这似乎没有解决问题。这是系统无法识别和添加“实验”标题的路径的问题吗?是否需要指定其他包含路径或编译器选项?
作为补充说明,我在Eclipse / CDT中注意到,在Project Explorer视图的“includes”选项卡下,它显示了系统标题路径的列表。它在/user/include/c++/4.4.4路径选项卡下列出了许多头文件,但它没有列出正则表达式头。我认为这也重申了设定问题。
答案 0 :(得分:0)
发现问题:
#include <regex>
应该是
#include <regex.h>
regex.h
标头是正则表达式的GNU C库实现,包含我尝试在源代码中使用的函数。 regex
标头是TR1(和不完整的)正则表达式实现。所以@Oli,毕竟我没有引用相关的标题!