(我知道类似的问题已在这里多次发布,但我正在发布一个新问题,因为提议的解决方案在我的情况下似乎不起作用!)
所以,我正在尝试编译一个使用boost :: program_options的非常简单的程序。我的boost库安装在/ usr / lib /。如果我输入ls /usr/lib/ | grep program_options
,我会收到以下信息:
libboost_program_options-mt.a
libboost_program_options-mt.so
libboost_program_options.a
libboost_program_options.so
libboost_program_options.so.1.46.1
所以lib就在那里。然而,当我尝试通过执行g++ -L/usr/lib/ -lboost_program_options main.cpp
进行编译时,我得到了很多“未定义的引用...”错误(我不认为在这里c / c它是有用的)。
如果我输入ld -lboost_program_options --verbose
,我就明白了:
attempt to open /usr/x86_64-linux-gnu/lib64/libboost_program_options.so failed
attempt to open /usr/x86_64-linux-gnu/lib64/libboost_program_options.a failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libboost_program_options.so failed
attempt to open /usr/local/lib/x86_64-linux-gnu/libboost_program_options.a failed
attempt to open /usr/local/lib64/libboost_program_options.so failed
attempt to open /usr/local/lib64/libboost_program_options.a failed
attempt to open /lib/x86_64-linux-gnu/libboost_program_options.so failed
attempt to open /lib/x86_64-linux-gnu/libboost_program_options.a failed
attempt to open /lib64/libboost_program_options.so failed
attempt to open /lib64/libboost_program_options.a failed
attempt to open /usr/lib/x86_64-linux-gnu/libboost_program_options.so failed
attempt to open /usr/lib/x86_64-linux-gnu/libboost_program_options.a failed
attempt to open /usr/lib64/libboost_program_options.so failed
attempt to open /usr/lib64/libboost_program_options.a failed
attempt to open /usr/local/lib/libboost_program_options.so failed
attempt to open /usr/local/lib/libboost_program_options.a failed
attempt to open /lib/libboost_program_options.so failed
attempt to open /lib/libboost_program_options.a failed
attempt to open /usr/lib/libboost_program_options.so succeeded
-lboost_program_options (/usr/lib/libboost_program_options.so)
libstdc++.so.6 needed by /usr/lib/libboost_program_options.so
found libstdc++.so.6 at /usr/lib/x86_64-linux-gnu/libstdc++.so.6
libgcc_s.so.1 needed by /usr/lib/libboost_program_options.so
found libgcc_s.so.1 at /lib/x86_64-linux-gnu/libgcc_s.so.1
libpthread.so.0 needed by /usr/lib/libboost_program_options.so
found libpthread.so.0 at /lib/x86_64-linux-gnu/libpthread.so.0
libc.so.6 needed by /usr/lib/libboost_program_options.so
found libc.so.6 at /lib/x86_64-linux-gnu/libc.so.6
libm.so.6 needed by /usr/lib/x86_64-linux-gnu/libstdc++.so.6
found libm.so.6 at /lib/x86_64-linux-gnu/libm.so.6
ld-linux-x86-64.so.2 needed by /usr/lib/x86_64-linux-gnu/libstdc++.so.6
found ld-linux-x86-64.so.2 at /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
ld: warning: cannot find entry symbol _start; not setting start address
所以它实际上找到了lib(在查看许多不同的地方之后),但是最后会出现一些奇怪的事情。
有什么想法吗?
修改 以下是“未定义的引用...”错误:
main.cpp:(.text+0x29): undefined reference to `boost::program_options::options_description::m_default_line_length'
main.cpp:(.text+0x35): undefined reference to `boost::program_options::options_description::m_default_line_length'
main.cpp:(.text+0x6e): undefined reference to `boost::program_options::options_description::options_description(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)'
main.cpp:(.text+0xa4): undefined reference to `boost::program_options::options_description::add_options()'
main.cpp:(.text+0xbe): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, char const*)'
main.cpp:(.text+0xd3): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
main.cpp:(.text+0xe2): undefined reference to `boost::program_options::variables_map::variables_map()'
main.cpp:(.text+0x13c): undefined reference to `boost::program_options::store(boost::program_options::basic_parsed_options<char> const&, boost::program_options::variables_map&, bool)'
为了完成,涉及boost的短代码:
options_description desc("Allowed options");
desc.add_options()
("help,h","Display help")
("file,f",value<string>(&dataFile),"Path to the file containing the data");
variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
当然#include <boost/program_options.hpp>
位于文件的开头。
答案 0 :(得分:1)
我认为您的编译/链接行不正确。
g++ -L/usr/lib/ -lboost_program_options main.cpp
应该是:
g++ -L/usr/lib/ main.cpp -lboost_program_options
有关详情,请查看以下答案:https://stackoverflow.com/a/409470/10077