【问题标题】:What causes the following linking error with the boost c++ libraries?是什么导致 boost c++ 库出现以下链接错误?
【发布时间】:2021-08-23 18:23:10
【问题描述】:

您好,我在 cygwin 上使用 gcc 编译器编译程序时出现链接错误。第一张图片是来自 boost 文件系统库教程页面的简单示例程序,其中我在 boost 文件夹中包含了 filesystem.hpp。下面是我尝试使用以下命令编译时链接器错误的图片:

g++ -I C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0 -I C:/Users/Ejer/Desktop/c++Dep/eigen-3.4.0 -L C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0/stage/lib test.cpp -o ser

在这里,我尝试使用 eigen 和 boost 库编译我的程序 test.cpp,并设置他们告诉我设置为使用 b2.exe 构建库后的路径的包含器路径。我还链接到 lib 文件以进行提升。我还尝试过专门链接到不同的文件系统库文件。提前致谢

#include <iostream>
#include <boost/filesystem.hpp>
using std::cout;
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    cout << "Usage: tut3 path\n";
    return 1;
  }

  path p (argv[1]);

  try
  {
    if (exists(p))
    {
      if (is_regular_file(p))
        cout << p << " size is " << file_size(p) << '\n';

      else if (is_directory(p))
      {
        cout << p << " is a directory containing:\n";

        for (directory_entry& x : directory_iterator(p))
          cout << "    " << x.path() << '\n';
      }
      else
        cout << p << " exists, but is not a regular file or directory\n";
    }
    else
      cout << p << " does not exist\n";
  }

  catch (const filesystem_error& ex)
  {
    cout << ex.what() << '\n';
  }

  return 0;
}

【问题讨论】:

  • 您是否在 cygwin 中使用相同的编译器构建了 boost? msvc 二进制文件将不起作用。
  • 是的,我用 gcc 运行引导文件,然后在 cygwin 中运行 b2.exe
  • 您没有向编译器提供 boost 文件系统库(只是可以找到它的目录)。在目录C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0/stage/lib中查找boost和filesystem asa库并添加到上面的命令行中
  • 我试过了。这就是我在我的问题中写的
  • 我在您的问题中看到您没有指定要链接的实际库。你告诉编译器头文件在哪里,链接器在哪里,但你没有链接到文件系统库。它不会像在 msvc 中使用 #pragma comment(lib, libname.lib) 那样自动执行此操作

标签: c++ gcc linker cygwin


【解决方案1】:

编译程序时出现链接错误

不,你没有。您在链接您的程序时遇到链接错误,而不是在编译时。

原因:您没有提供库(-L C:/Users/.... 告诉链接器在哪里搜索库;而不是链接哪些库)。您的命令行应该类似于:

g++ -I ... -L ... test1.cpp -o ser -lboost_filesystem

【讨论】:

  • 我刚刚尝试按照你说的做,链接特定库但我仍然得到cannot find -llibboost_filesystem-vc142-mt-x32-1_77.lib collect2: error: ld returned 1 exit status,这是我尝试过的命令:g++ -I C:/Users/Ejer/Desktop/c++/c++Dep/boost_1_77_0 -L C:/Users/Ejer/Desktop/c++/c++Dep/boost_1_77_0/stage/lib/ -l libboost_filesystem-vc142-mt-x32-1_77.lib main.cpp -o ser
  • @SamuelCadell 库应该在之后 main.cpp,而不是在它之前。 eli.thegreenplace.net/2013/07/09/…。你也想要-lboost_filesystem-vc142-mt-x32-1_77,而不是-l libboost_filesystem-vc142-mt-x32-1_77.lib
猜你喜欢
  • 2022-12-18
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多