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

发布于 2024-09-12 00:40:45 字数 2876 浏览 14 评论 0原文

我正在尝试编译 multiple_sources.cpp在我的电脑上编译。我正在运行完全更新的 Xubuntu Lucid Lynx。

它可以使用 g++ -c multiple_sources.cpp 毫无问题地进行编译,但是当我尝试使用 g++ multiple_sources.o 链接并生成可执行文件时,我得到:

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

  • 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

我做错了什么?

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

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

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

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2024-09-19 00:40:45

-l 参数错误;去掉 lib 前缀并使用 -lboost_program_options

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

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文