【发布时间】: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) 那样自动执行此操作