如何构建Boost :: program_options(在linux上)

时间:2010-12-27 01:04:40

标签: c++ boost build boost-program-options

我正在尝试按照官方说明使用boost::program_optionshttp://www.boost.org/doc/libs/1_36_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library

但它不起作用:

~/download/boost_1_36_0/libs/program_options/example> g++ -o first first.cpp /usr/lib64/libboost_program_options-mt-1_36.a

/tmp/ccNh69JH.o: In function `main':
first.cpp:(.text+0xc8): undefined reference to `boost::program_options::options_description::options_description(std::basic_string, std::allocator > const&, unsigned int, unsigned int)'
/tmp/ccNh69JH.o: In function `std::basic_string, std::allocator > const& boost::program_options::validators::get_single_string(std::vector, std::allocator >, std::allocator, std::allocator > > > const&, bool)':                                                                     
first.cpp:(.text._ZN5boost15program_options10validators17get_single_stringIcEERKSbIT_St11char_traitsIS3_ESaIS3_EERKSt6vectorIS7_SaIS7_EEb[std::basic_string, std::allocator > const& boost::program_options::validators::get_single_string(std::vector, std::allocator >, std::allocator, std::allocator > > > const&, bool)]+0x142): undefined reference to `boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&)'
first.cpp:(.text._ZN5boost15program_options10validators17get_single_stringIcEERKSbIT_St11char_traitsIS3_ESaIS3_EERKSt6vectorIS7_SaIS7_EEb[std::basic_string, std::allocator > const& boost::program_options::validators::get_single_string(std::vector, std::allocator >, std::allocator, std::allocator > > > const&, bool)]+0x2e9): undefined reference to `boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&)'
collect2: ld returned 1 exit status

这有效,但很奇怪:

g++ -o first first.cpp /usr/lib64/libboost_program_options.so.1.42.0

2 个答案:

答案 0 :(得分:2)

假设你已经包含了标题,编译了库并正确添加了对它们的引用,我可以建议你快速(和懒惰)的方法来解决问题。您可以将源文件添加到项目中,并在解决问题时将它们一起编译

答案 1 :(得分:1)

我创建了一个包含以下内容的boost.pc文件:

prefix=/usr/local/boost
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: boost
Description: my boost pc file
Version: 1.57.0
Cflags: -I${includedir}
Libs: -L${libdir} -lboost_system -lboost_program_options

现在我可以运行像

这样的东西
g++ -g -O2 `pkg-config ./boost.pc --cflags --libs` -w -c main-new.cpp -o obj/main.o
g++ -g -O2 -w obj/main.o -o bin/main `pkg-config ./boost.pc --cflags --libs`

(通过makefile)