【问题标题】:compile error even though file was included即使包含文件也编译错误
【发布时间】:2026-02-23 00:30:01
【问题描述】:

当我在 Unix 上编译我的 C++11 时,我收到以下错误:(即使它在我的 mac 的 clion 上运行良好)

-bash-4.2$ g++ -std=c++11 -Wall -Werror -pedantic-errors -DNDEBUG main.cpp utilities.cpp utilities.h Graph.cpp Graph.h Exception.cpp Exception.h Edge.cpp Edge.h Calculator.cpp Calculator.h -o final
/tmp/ccLJAsey.o: In function `inner_load(int, std::string&, Calculator&)':
Calculator.cpp:(.text+0x13aa): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::regex_iterator(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, std::basic_regex<char, std::regex_traits<char> > const&, std::bitset<11ul>)'
Calculator.cpp:(.text+0x13b9): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::regex_iterator()'
Calculator.cpp:(.text+0x13d2): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::regex_iterator(std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> > const&)'
Calculator.cpp:(.text+0x13e6): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::operator->()'
Calculator.cpp:(.text+0x1419): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::operator*()'
Calculator.cpp:(.text+0x14ae): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::operator->()'
Calculator.cpp:(.text+0x14cd): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::operator->()'
Calculator.cpp:(.text+0x1533): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::operator->()'
Calculator.cpp:(.text+0x1552): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::operator->()'
Calculator.cpp:(.text+0x1586): undefined reference to `std::regex_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, char, std::regex_traits<char> >::operator->()'

我已经包含了正则表达式,这是什么原因造成的?

【问题讨论】:

  • 这些是链接器错误。您已经包含了所有需要的头文件,但尚未向链接器提供实现头文件的库。
  • 为什么要编译*.h 头文件?
  • @user4581301 我改用 g++5.5,虽然它修复了错误,但我仍然收到以下错误:./final: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21'未找到(./final 要求)是什么意思?
  • 时髦。你只是做了我要建议的事情。早期的 Regex 实现非常愚蠢。看看您是否可以获得更多最新信息。最新的 GCC 是 10.2。至于新的错误,我不熟悉。您使用新编译器构建的代码可能试图针对旧库或其他一些愚蠢的东西运行。您可能需要静态链接标准库。
  • 看看这个问题是否符合你的问题:*.com/questions/19386651/…

标签: c++ c++11 compiler-errors compilation


【解决方案1】:

请记住,C++'#include &lt;regex&gt;(或其他任何东西)只是将(文本,源代码)文件/usr/include/.../regex/(... 将取决于您的确切安装)包含到正在编译的文件中.如果你看一下这些文件,它们通常包含类、函数、模板和各种#defines 的声明。实现所有其他地方的代码。标准头文件中的大部分内容都在标准 C++ 库中,并且在链接编译器时会自动将其添加进去。如果它不是标准部分的一部分,您可能需要在链接步骤中添加其他库。默认情况下包含的内容(在某种程度上)取决于编译器,也有可能一个编译器将完整的实现放入头文件(通过inlinefunctions、宏和其他东西),而另一个将它放在标准库中或单独的库。

【讨论】:

  • 请您看一下新的错误信息吗?
  • @dure,请不要更改问题。问一个新的。这可能是由于在混合中使用旧编译器编译的部分。从头开始清理和编译所有内容。