链接/编译使用boost / filesystem.hpp的程序

时间:2013-11-13 03:32:15

标签: c++ boost filesystems

我正在尝试在我正在编写的一些代码中使用boost / filesystem库。我似乎很难将其编译。我正在运行Debian Wheezy,并且已经升级版本1.49(如果你使用apt-get安装就会出现这种情况)。我正在尝试编译文档中提供的示例

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

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

我使用以下命令:

g++ temp.cc -o temp /usr/lib/libboost_filesystem.a

我收到了许多错误,例如:

/usr/lib/libboost_filesystem.a(operations.o): In function `boost::filesystem3::detail::dir_itr_close(void*&, void*&)':
(.text+0x4d): undefined reference to `boost::system::system_category()'
/usr/lib/libboost_filesystem.a(operations.o): In function `boost::filesystem3::detail::directory_iterator_increment(boost::filesystem3::directory_iterator&, boost::system::error_code*)':
(.text+0xe3): undefined reference to `boost::system::system_category()'

这可能是一些链接错误对吗?关于如何解决它的任何想法?

更新#1 : 我尝试使用-lboost_filesyste和-L / usr / lib运行它。它给了我以下错误:

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'

3 个答案:

答案 0 :(得分:22)

您没有正确链接库。另外,正如其他人所提到的,boost_filesystem也需要boost_system库。使用:

g++ temp.cc -o temp -lboost_system -lboost_filesystem

命令行参数-l foo链接libfoo.a库。如果静态库不在默认库位置,请使用命令-L /custom/library/dir。但我相信GCC会自动考虑/usr/lib


修改

根据您在下方的评论,您似乎没有使用main()功能编译文件,或者您的main()名称中有拼写错误。确保temp.cc只包含其中一项功能:

int main();
int main(int argc, char** argv);

当然你确实记得上/下案件很重要。 :)

答案 1 :(得分:2)

Boost.Filesystem使用Boost.System中的东西。你也必须与之相关联。

您看到的错误消息:

/usr/lib/libboost_filesystem.a(operations.o): In function 
`boost::filesystem3::detail::dir_itr_close(void*&, void*&)':
(.text+0x4d): undefined reference to `boost::system::system_category()'

这是对Boost.System的引用

添加-lboost_system你应该好好去(或者至少更好)。

答案 2 :(得分:1)

使用-lboost_filesystem

进行编译
相关问题