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

发布于 2024-10-08 22:18:20 字数 2324 浏览 4 评论 0原文

我正在尝试按照官方说明使用 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

I'm trying to use boost::program_options following the official instruction: http://www.boost.org/doc/libs/1_36_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library

But it doesn't work:

~/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

This works but it is weird:

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

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

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

发布评论

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

评论(2

再浓的妆也掩不了殇 2024-10-15 22:18:20

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

Assuming you've included the header, compiled the libraries and added a reference to them correctly, I can suggest you a fast (and lazy) way to solve the problem. You can add the source files to the project and compile them together while you work the solution out

神魇的王 2024-10-15 22:18:20

我创建了一个包含以下内容的 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)

I've created a boost.pc file with following contents:

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

Now I can run something like

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`

(via makefile)

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