g++没有从regex系统头文件中找到符号

G++ not finding symbols from regex system header

本文关键字:文件 符号 系统 regex g++      更新时间:2023-10-16

我试图在我的c++项目中使用GNU C库regex功能,特别是我试图使用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

我意识到regex头文件是TR1实现,因此对于我的GCC版本来说是实验性的。我根据第一次尝试编译时收到的编译器警告添加了-std=gnu++0x编译器选项,但这似乎并没有解决问题。这是系统不识别和添加路径到"实验"头的问题吗?是否有需要指定的附加包含路径或编译器选项?

作为额外的注意事项,我注意到在Eclipse/CDT中,在Project Explorer视图中的"includes"选项卡下,它显示了系统头路径的列表。它列出了/user/include/c++/4.4.4 path选项卡下的许多头文件,但它没有列出regex头文件。

发现问题:

#include <regex>
应该

#include <regex.h>

regex.h头是正则表达式的GNU C库实现,包含我试图在我的源代码中使用的函数。regex头是TR1(和不完整)正则表达式的实现。所以@Oli,我毕竟没有引用相关的标题!