【问题标题】:Importing cstring fails: include <cstring> error导入 cstring 失败:include <cstring> 错误
【发布时间】:2018-11-11 20:15:17
【问题描述】:

我正在 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 框架。没用,所以我写了一些小程序 这(我认为)显示了我的问题。我不能包含&lt;cstring&gt;

#include <cstring>

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

使用g++ -Wall a.cpp -o atest 编译时,我得到了2500 row error message

现在我认为 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++有问题?

我已经快要设置好我的系统了 从头开始,所以你们是我最后的希望。

【问题讨论】:

  • 从命令返回的内容 - which g++
  • 那些很奇怪。你的/usr/include/string.h 被篡改了吗?第一个错误提示未关闭的extern ”C” 块。 (第一个错误总是最重要的。)
  • 如前所述,/usr/include/string.h 文件似乎已损坏。它不应包含 iostream 标头。我会对那个文件的出处感兴趣。如果这个文件是旧的并且你的代码之前工作过,那么原因可能是之前实际上没有包含该文件,因为使用了其他文件。
  • 一个疯狂的猜测是“某人”试图通过打印参数来检测涉及strlen 的问题(也许是你的“朋友”?)#includecout线不应该在那里。
  • @Belorenz 我建议您使用 C 头文件 libc6-dev 重新安装 Ubuntu 软件包,这样您就可以确定它们没问题。 (我真的建议不要编辑系统标头。事实上,我建议不要使用 sudo 编辑 /etc 之外的任何内容,除非您真的确切地知道自己在做什么。) 感谢 molbdnilo 和 Konrad Rudolph 捕捉到这么快就开始了。

标签: c++ c-strings


【解决方案1】:

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

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

sudo apt-get install libc6-dev --reinstall

它仍然有效。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 2014-10-20
    • 2019-01-25
    • 2021-12-22
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多