【问题标题】:read was not declared error message读取未声明错误消息
【发布时间】:2013-08-06 13:13:08
【问题描述】:

我正在交叉编译一个包(libqmi)。(通过简单的编译就可以了。)

当我尝试遵守 c++ 部分时,问题就出现了。 我收到的消息是

"read is not declared".

我知道在 C 的情况下不需要包含它,但是 C++ 呢?

我尝试手动添加标题:fcntl.hunistd.h 也没有任何解决方案。 (编译器找到并包含,但错误信息仍然存在)

您知道这背后的问题是什么吗? 我不认为这个问题是错误的,因为它是一个已实现且良好的宿主编译器。

编辑: 感谢 cmets。 主机:Linux、x86、 目标:Linux、手臂

unistd.h 头文件没有解决问题: 我也试过type alloc,可能有misalloc。 GobiQMICore.cpp: 在成员函数'virtual std::vector, std::basic_string >> cGobiQMICore::GetAvailableDevices()'中: GobiQMICore.cpp:319:39:错误:未在此范围内声明“读取” GobiQMICore.cpp:334:21: 错误: 'close' 未在此范围内声明

代码:

/*===========================================================================
METHOD:
   GetAvailableQDLPorts (Public Method)

DESCRIPTION:
   Return the set of available Gobi QDL ports

RETURN VALUE:
   std::vector <sDeviceID>
===========================================================================*/
std::vector <std::string> cGobiQDLCore::GetAvailableQDLPorts()
{
   std::vector <std::string> devices;

   std::string path = "/sys/bus/usb/devices/";

   std::vector <std::string> files;
   DepthSearch( path,
                2,
                "ttyUSB",
                files );

   int fileNum = files.size();
   for (int i = 0; i < fileNum; i++)
   {
      // Example "/sys/bus/usb/devices/8-1/8-1:1.1/ttyUSB0"
      std::string nodePath = files[i];

      int lastSlash = nodePath.find_last_of( "/" );

      // This is what we want to return if everything else matches
      std::string deviceNode = nodePath.substr( lastSlash + 1 );

      // Move down one directory to the interface level
      std::string curPath = nodePath.substr( 0, lastSlash );

      // Read bInterfaceNumber
      int handle = open( (curPath + "/bInterfaceNumber").c_str(), 
                         O_RDONLY );
      if (handle == -1)
      {
         continue;
      }

      char buff[4];
      memset( buff, 0, 4 );

      bool bFound = false;
      int ret = read( handle, buff, 2 );
      if (ret == 2)
      {
         // Interface 1 or 0
         ret = strncmp( buff, "01", 2 );
         if (ret == 0)
         {
            bFound = true;
         }
         ret = strncmp( buff, "00", 2 );
         if (ret == 0)
         {
            bFound = true;
         }

      }
      close( handle );

      if (bFound == false)
      {
         continue;
      }

      // Move down one directory to the device level
      curPath = curPath.substr( 0, curPath.find_last_of( "/" ) );

      // Read idVendor
      handle = open( (curPath + "/idVendor").c_str(), O_RDONLY );
      if (handle == -1)
      {
         continue;
      }
      bFound = false;
      ret = read( handle, buff, 4 );
      if (ret == 4)
      {
         ret = strncmp( buff, "05c6", 4 );
         if (ret == 0)
         {
            bFound = true;
         }
      }
      close( handle );

      if (bFound == false)
      {
         continue;
      }

      // Read idProduct
      handle = open( (curPath + "/idProduct").c_str(), O_RDONLY );
      if (handle == -1)
      {
         continue;
      }
      bFound = false;
      ret = read( handle, buff, 4 );
      if (ret == 4)
      {
         ret = strncmp( buff, "920c", 4 );
         if (ret == 0)
         {
            bFound = true;
         }
      }
      close( handle );

      if (bFound == false)
      {
         continue;
      }

      // Success!
      devices.push_back( deviceNode );
   }

   return devices;
}

T

【问题讨论】:

  • 交叉编译的目标是什么?我假设 Linux 是主机?
  • 您的问题应该通过包含 unistd.h 来解决。正如您所说的,您已经包含了,所以请确保您将其包含在所需且正确的 c 文件中。
  • 显示一些带有read()的代码!
  • 我刚刚修改了问题,添加了一些附加信息,根据您的 cmets。谢谢
  • 您向我们展示的代码没有#include 指令,因此它应该为您提供read 以及std::DepthSearch 等的错误。你说你的代码中有#include &lt;unistd.h&gt;向我们展示该代码。向我们展示与您的真实代码相似的内容是没有用的。

标签: c++ c linux compiler-construction compiler-errors


【解决方案1】:

我知道在 C 的情况下不需要包含它,但是 C++ 呢?

我认为您应该始终包含您需要的标题。我猜您的编译器正在为您完成这项工作,如果您在 gcc 中使用“-Wall”参数,您应该会收到警告。

在 Linux 下,要知道需要包含什么标头,只需键入 man function。有时您可能会看到 bash 手册页,要打开,您需要指出 man 2 read 部分,并且在概要中,您有所需的标题。要获取这些手册页,您还需要在基于 Debian 的发行版上安装 manpages-dev。

为了回答您的问题,我在使用命名空间编写 C++ 程序时也遇到过这种问题。如果您在命名空间内,请尝试像 ::read(...) 那样调用此函数

【讨论】:

  • 感谢评论是的,我也这么认为。但是我真的很有趣,为什么如果我使用我的主机编译器我没有得到任何错误。我简单地键入 ./configure 和 make 没有任何参数并且没有错误。如果是交叉编译器,我会得到上面的东西。我害怕操纵代码会导致我们以后表现不佳(这是第三方代码,我没有时间理解这么多我可以重构)或者其他事情可能存在严重的配置错误,这可能会导致以后出现其他问题.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多