找不到 C++链接库时,编译 `boost::program_options` 示例时出错
我正在尝试编译 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-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 thelib
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
.