首页 > 解决方案 > 导入 cstring 失败:包括错误

问题描述

我正在 Ubuntu 16.04 上使用 ROS 和 C++ 开展一个小组项目。一切都很好,直到两周前我突然收到了这个错误消息:

In file included from /usr/include/c++/5/cstring:42:0,
         from /usr/include/boost/math/special_functions/detail/fp_traits.hpp:23,
         from /usr/include/boost/math/special_functions/fpclassify.hpp:20,
         from /usr/include/boost/math/special_functions/round.hpp:16,
         from /opt/ros/kinetic/include/ros/time.h:58,
         from /opt/ros/kinetic/include/ros/ros.h:38,
         from /home/benjamin/Documents/bob_ws/src/tutorial/hello_world/src/hello_world.cpp:1:
/usr/include/string.h: In function ‘size_t strlen(const char*)’:
/usr/include/string.h:395:6: error: ‘cout’ was not declared in this scope
      cout << *__s;
      ^
/usr/include/string.h:395:6: note: suggested alternative:
In file included from /opt/ros/kinetic/include/ros/time.h:54:0,
         from /opt/ros/kinetic/include/ros/ros.h:38,
         from /home/benjamin/Documents/bob_ws/src/tutorial/hello_world/src/hello_world.cpp:1:
/usr/include/c++/5/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
          ^
In file included from /usr/include/features.h:367:0,
         from /usr/include/stdlib.h:24,
         from /opt/ros/kinetic/include/ros/platform.h:53,
         from /opt/ros/kinetic/include/ros/time.h:53,
         from /opt/ros/kinetic/include/ros/ros.h:38,
         from /home/benjamin/Documents/bob_ws/src/tutorial/hello_world/src/hello_world.cpp:1:
/usr/include/string.h:396:6: error: expected primary-expression before ‘)’ token
      __THROW __attribute_pure__ __nonnull ((1));

作为 C++ 和 ROS 的新手,我的第一个想法是我安装/删除一些软件包搞砸了。所以我尝试重新安装整个 ros 框架。没用,所以我写了一个小程序(我认为)显示了我的问题。我不能包括<cstring>

#include <cstring>

int main(int argc, char** argv)
{
  return 0;
}

编译时,g++ -Wall a.cpp -o atest我收到了2500 行错误消息

现在我认为 cstring 可能有一点问题,但遗憾的是我不知道如何解决它。我的谷歌搜索表明这locate cstring | grep include可能很有趣:

/usr/include/boost/compatibility/cpp_c_headers/cstring
/usr/include/boost/python/docstring_options.hpp
/usr/include/boost/test/utils/basic_cstring
/usr/include/boost/test/utils/basic_cstring/basic_cstring.hpp
/usr/include/boost/test/utils/basic_cstring/basic_cstring_fwd.hpp
/usr/include/boost/test/utils/basic_cstring/bcs_char_traits.hpp
/usr/include/boost/test/utils/basic_cstring/compare.hpp
/usr/include/boost/test/utils/basic_cstring/io.hpp
/usr/include/c++/4.8/cstring
/usr/include/c++/5/cstring
/usr/include/qt4/Qt/q3cstring.h
/usr/include/qt4/Qt3Support/q3cstring.h

也许安装了两个版本的C++是个问题?

我非常接近从头开始设置我的系统,所以你们是我最后的希望。

标签: c++c-strings

解决方案


感谢@molbdnilo 和@Konrad Rudolph,我发现我的/usr/include/string.h(这是一个C 头文件)试图导入/usr/include/c++/5/iostream(这是一个c++ 库)。在一个工作的 vim diff 之后,string.h我删除了不同的行,这只是 iostream 的包含语句。

正如@digitalarbeiter 建议的那样,手动更改此类文件中的某些代码通常是个坏主意,因此我重新安装libc6-dev

sudo apt-get install libc6-dev --reinstall

它仍然有效。

我仍然不知道这怎么会发生。该文件在 4 月编辑,错误发生在 10 月。无论如何感谢所有的帮助者!


推荐阅读