链接时找不到C ++库,错误编译`boost :: program_options`示例

时间:2010-07-28 16:59:51

标签: c++ boost boost-program-options

我正在尝试编译multiple_sources.cpp以在我的计算机上编译。我正在运行Xubuntu Lucid Lynx全面更新。

它会在g++ -c multiple_sources.cpp编译时没有问题,但当我尝试链接并与g++ multiple_sources.o进行exectuable时,我得到:

multiple_sources.cpp:(.text+0x3d): undefined reference to `boost::program_options::options_description::m_default_line_length'
multiple_sources.cpp:(.text+0x87): undefined reference to `boost::program_options::options_description::options_description(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)'
multiple_sources.cpp:(.text+0x15a): undefined reference to `boost::program_options::options_description::add_options()'
multiple_sources.cpp:(.text+0x17b): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, char const*)'
multiple_sources.cpp:(.text+0x193): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, char const*)'
multiple_sources.cpp:(.text+0x1af): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
multiple_sources.cpp:(.text+0x1eb): undefined reference to `boost::program_options::options_description::m_default_line_length'
multiple_sources.cpp:(.text+0x252): undefined reference to `boost::program_options::options_description::options_description(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)'
...

et cetera ad nauseum

我确实安装了库:

>ls -l /usr/lib/libboost_program_options*
-rw-r--r-- 1 root root 640800 2010-03-31 21:19 /usr/lib/libboost_program_options.a
lrwxrwxrwx 1 root root     26 2010-04-09 00:57 /usr/lib/libboost_program_options-mt.a     -> libboost_program_options.a
lrwxrwxrwx 1 root root     34 2010-04-09 00:57 /usr/lib/libboost_program_options-mt.so -> libboost_program_options.so.1.40.0
lrwxrwxrwx 1 root root     34 2010-04-09 00:57 /usr/lib/libboost_program_options.so -> libboost_program_options.so.1.40.0
-rw-r--r-- 1 root root 289336 2010-03-31 21:19 /usr/lib/libboost_program_options.so.1.40.0

在阅读g ++手册页后,我也尝试过:

  • g++ -llibboost_program_options multiple_sources.cpp
  • g++ -llibboost_program_options.a multiple_sources.cpp
  • g++ -llibboost_program_options.so multiple_sources.cpp
  • -L/usr/lib 之前
  • 以及-l 以上的所有内容

他们都因以下变化而失败:

/usr/bin/ld: cannot find -llibboost_program_options.so
collect2: ld returned 1 exit status

我做错了什么?

1 个答案:

答案 0 :(得分:25)

-l param错了;摆脱lib预先修复并使用-lboost_program_options

链接器期望所有库都以lib开头,并且在指定库时将其保留为关闭。

您还可以在文件列表中包含库的完整路径,而不包含-l(例如g++ multiple_sources.cpp /usr/lib/libboost_program_options.so),但对于系统库,最好遵循惯例;它们并不总是安装在/usr/lib