【发布时间】:2013-05-31 16:26:20
【问题描述】:
我正在尝试在我的 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 视图的“包含”选项卡下,它显示了系统头文件路径的列表。它在 /user/include/c++/4.4.4 路径选项卡下列出了许多头文件,但没有列出正则表达式头。我认为这也再次确认了存在设置问题。
【问题讨论】:
-
只是一个健全的检查;你的源文件肯定是#include了相关的头文件?
-
是的,GCC 还没有实现
<regex>。 -
大声笑,是的,Gcc 4.4.4 已经很老了。不幸的是,我的公司有严格的 IT 要求,并且严格遵循 RHEL 分发更改生命周期。它们实际上不允许我们进行太多定制。
-
错误中的所有符号都不是命名空间限定的 - 您的代码中是否有适当的“使用命名空间 tr1”?