0

尝试在我的代码中静态链接 boost 程序选项库时,我在链接器尝试链接它时遇到错误。

gmake[1]: Leaving directory `tools'
g++ -rdynamic -g  -o test test.o -Lboost_1_39_0/lib/Linux64//libboost_program_options-gcc41-mt-1_39.a … (other dependent libs)
test.o: In function `basic_config_file_iterator':
 boost_1_39_0/include/Linux64/boost-1_39/boost/program_options/detail/config_file.hpp:147: undefined reference to `boost::program_options::detail::common_config_file_iterator::common_config_file_iterator(std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool)'
 boost_1_39_0/include/Linux64/boost-1_39/boost/program_options/detail/config_file.hpp:150: undefined reference to `boost::program_options::detail::common_config_file_iterator::get()'
test.o: In function `boost::eof_iterator<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> >::increment()':
 boost_1_39_0/include/Linux64/boost-1_39/boost/program_options/eof_iterator.hpp:75: undefined reference to `boost::program_options::detail::common_config_file_iterator::get()'
test.o: In function `boost::program_options::detail::basic_config_file_iterator<char>::getline(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
 boost_1_39_0/include/Linux64/boost-1_39/boost/program_options/detail/config_file.hpp:163: undefined reference to `boost::program_options::to_internal(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: ld returned 1 exit status
gmake: *** [test] Error 1

另外,当我对这些函数的 libboost_program_options-gcc41-mt-1_39.a 进行 NM 时,我得到低于输出

$ nm -goC libboost_program_options-gcc41-mt-1_39.a | grep -i common_config_file_iterator
libboost_program_options-gcc41-mt-1_39.a:config_file.o:00000000000002e0 T boost::program_options::detail::common_config_file_iterator::add_option(char const*)
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000eb0 T boost::program_options::detail::common_config_file_iterator::get()
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 W boost::program_options::detail::common_config_file_iterator::getline(std::string&)
libboost_program_options-gcc41-mt-1_39.a:config_file.o:00000000000007f0 T boost::program_options::detail::common_config_file_iterator::common_config_file_iterator(std::set<std::string, std::less<std::string>, std::allocator<std::string> > const&, bool)
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000b80 T boost::program_options::detail::common_config_file_iterator::common_config_file_iterator(std::set<std::string, std::less<std::string>, std::allocator<std::string> > const&, bool)
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 W boost::program_options::detail::common_config_file_iterator::~common_config_file_iterator()
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 W boost::program_options::detail::common_config_file_iterator::~common_config_file_iterator()
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000060 T boost::program_options::detail::common_config_file_iterator::allowed_option(std::string const&) const
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 V typeinfo for boost::eof_iterator<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> >
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 V typeinfo for boost::iterator_facade<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> const, boost::forward_traversal_tag, boost::program_options::basic_option<char> const&, long>
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 V typeinfo for boost::program_options::detail::common_config_file_iterator
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 V typeinfo name for boost::eof_iterator<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> >
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 V typeinfo name for boost::iterator_facade<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> const, boost::forward_traversal_tag, boost::program_options::basic_option<char> const&, long>
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 V typeinfo name for boost::program_options::detail::common_config_file_iterator
libboost_program_options-gcc41-mt-1_39.a:config_file.o:0000000000000000 V vtable for boost::program_options::detail::common_config_file_iterator
libboost_program_options-gcc41-mt-1_39.a:parsers.o:                 U boost::program_options::detail::common_config_file_iterator::get()
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 W boost::program_options::detail::common_config_file_iterator::getline(std::string&)
libboost_program_options-gcc41-mt-1_39.a:parsers.o:                 U boost::program_options::detail::common_config_file_iterator::common_config_file_iterator(std::set<std::string, std::less<std::string>, std::allocator<std::string> > const&, bool)
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 W boost::program_options::detail::common_config_file_iterator::~common_config_file_iterator()
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 W boost::program_options::detail::common_config_file_iterator::~common_config_file_iterator()
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 W boost::program_options::detail::common_config_file_iterator::~common_config_file_iterator()
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 V typeinfo for boost::eof_iterator<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> >
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 V typeinfo for boost::iterator_facade<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> const, boost::forward_traversal_tag, boost::program_options::basic_option<char> const&, long>
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 V typeinfo for boost::program_options::detail::common_config_file_iterator
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 V typeinfo name for boost::eof_iterator<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> >
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 V typeinfo name for boost::iterator_facade<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> const, boost::forward_traversal_tag, boost::program_options::basic_option<char> const&, long>
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 V typeinfo name for boost::program_options::detail::common_config_file_iterator
libboost_program_options-gcc41-mt-1_39.a:parsers.o:0000000000000000 V vtable for boost::program_options::detail::common_config_file_iterator

我不希望在我的测试程序中使用动态绑定,因此如果我在进行链接时缺少要包含的任何库,则需要任何指针。

4

2 回答 2

0

问题是我链接到我的程序的方式。我正在做如下

g++ -g test.cxx -o test -L/<path>/boost_1_39_0/lib/Linux64//libboost_program_options-gcc41-mt-1_39.a

而我们需要做如下

g++ -g test.cxx -o test /<path>/boost_1_39_0/lib/Linux64//libboost_program_options-gcc41-mt-1_39.a

这将解决问题。

-阿比纳夫

于 2012-09-05T14:03:17.717 回答
0

您正在使用编译器开关-L错误。在 gcc -L中用于指定要搜索库的目录。但是,这意味着如果您没有在附加标志-l之后指定库名称,它将无法链接该库以形成您想要的可执行文件。

因此,在这种情况下,您将执行以下操作:

g++ -g test.cxx -o test -L/<path>/boost_1_39_0/lib/Linux64/ -lboost_program_options-gcc41-mt-1_39
于 2013-11-28T18:32:28.517 回答