简体   繁体   English

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

[英]Cannot find C++ library when linking, error compliling the `boost::program_options` example

I am trying to compile the multiple_sources.cpp to compile on my computer. 我正在尝试编译multiple_sources.cpp以在我的计算机上编译。 I am running Xubuntu Lucid Lynx fully updated. 我正在运行Xubuntu Lucid Lynx全面更新。

It will compile without issue with g++ -c multiple_sources.cpp but when I try to link and make an exectuable with g++ multiple_sources.o I get: 它会在没有问题的情况下编译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 . et cetera ad nauseum

I do have the library installed: 我确实安装了库:

>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

After reading the g++ man page, I have also tried: 在阅读g ++手册页后,我也尝试过:

  • g++ -llibboost_program_options multiple_sources.cpp
  • g++ -llibboost_program_options.a multiple_sources.cpp
  • g++ -llibboost_program_options.so multiple_sources.cpp
  • and all of the above with -L/usr/lib before the -l 以及-L/usr/lib 之前的所有-l

They all fail with a variation on: 他们都失败了变化:

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

What am I doing wrong? 我究竟做错了什么?

The -l param is wrong; -l param是错误的; get rid of the lib pre-fix and use -lboost_program_options . 摆脱lib pre-fix并使用-lboost_program_options

The linker expects all libraries to begin with lib , and for you to leave that off when specifying the library. 链接器期望所有库都以lib开头,并且在指定库时让您关闭它。

You could also include the full path to the library in the list of files, without -l (eg g++ multiple_sources.cpp /usr/lib/libboost_program_options.so ), but for system libraries, it's better to follow convention; 您还可以在文件列表中包含库的完整路径,不包含-l (例如g++ multiple_sources.cpp /usr/lib/libboost_program_options.so ),但对于系统库,最好遵循约定; they aren't always installed in /usr/lib . 它们并不总是安装在/usr/lib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM