链接/编译使用boost/filesystem.hpp的程序 [英] Linking/compiling a program that uses boost/filesystem.hpp
问题描述
我正在尝试在我编写的某些代码中使用boost/filesystem库.我似乎很难编译它.我正在运行Debian Wheezy,并且具有增强版1.49(如果使用apt-get安装,这将是该版本).我正在尝试编译文档中提供的示例
I'm trying to use the boost/filesystem library in some code that I am writing. I seem to be having a hard time getting it to compile. I'm running Debian Wheezy, and have boost version 1.49(which is what comes if you install using apt-get). I'm trying to compile an example that is available with the documentation
#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
我遇到许多错误,例如:
I get a number of errors such as:
/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()'
这可能是一些链接错误,对吗?关于如何解决这个问题有什么想法吗?
This is probably some linking error right? Any ideas on how I could solve it?
更新#1 : 我尝试使用-lboost_filesyste和-L/usr/lib运行它.它给了我以下错误:
UPDATE #1: I tried running it with the -lboost_filesyste and -L /usr/lib. It gives me the following error:
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
推荐答案
您没有正确链接库.另外,如其他人所述,boost_filesystem
也需要boost_system
库.使用:
You are not linking the library properly. Also, as others mentioned, boost_filesystem
needs also boost_system
library. Use:
g++ temp.cc -o temp -lboost_system -lboost_filesystem
命令行参数-l foo
链接libfoo.a
库.如果静态库不在默认库位置,请使用命令-L /custom/library/dir
.但我相信GCC会自动考虑/usr/lib
.
Command line param -l foo
links libfoo.a
library. If the static library is not in default library location, use command -L /custom/library/dir
. But I believe /usr/lib
is automatically taken into consideration by GCC.
根据下面的注释,您似乎没有使用main()
函数编译文件,或者您在main()
名称中有错字.确保temp.cc
仅包含以下功能之一:
According to your comment below it looks like you are not compiling the file with main()
function, or you have a typo in main()
name. Make sure that temp.cc
contains one and only one of these functions:
int main();
int main(int argc, char** argv);
当然,您确实记得大小写很重要. :)
Of course you do remember that upper/lower case matters. :)
这篇关于链接/编译使用boost/filesystem.hpp的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!