链接时找不到C ++库,错误编译了`boost :: program_options`示例 [英] Cannot find C++ library when linking, error compliling the `boost::program_options` example

查看:180
本文介绍了链接时找不到C ++库,错误编译了`boost :: program_options`示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to compile the multiple_sources.cpp to compile on my computer. I am running Xubuntu Lucid Lynx fully updated.

它将与g++ -c multiple_sources.cpp一起编译而没有问题,但是当我尝试使用g++ multiple_sources.o链接并使其可执行时,我得到:

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:

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)'
...

其他令人厌恶的动物.

我确实安装了库:

>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 ++手册页后,我也尝试过:

After reading the g++ man page, I have also tried:

  • 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
  • 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

它们都因以下原因而失败:

They all fail with a variation on:

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

我在做什么错了?

推荐答案

-l参数错误;删除lib前缀并使用-lboost_program_options.

The -l param is wrong; get rid of the lib pre-fix and use -lboost_program_options.

链接器希望所有库都以lib开头,并且您希望在指定库时将其保留.

The linker expects all libraries to begin with lib, and for you to leave that off when specifying the library.

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

You could also include the full path to the library in the list of files, without -l (e.g. g++ multiple_sources.cpp /usr/lib/libboost_program_options.so), but for system libraries, it's better to follow convention; they aren't always installed in /usr/lib.

这篇关于链接时找不到C ++库,错误编译了`boost :: program_options`示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆