首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在linux上使用Boost/Json

如何在linux上使用Boost/Json
EN

Stack Overflow用户
提问于 2022-03-14 15:20:34
回答 2查看 1.3K关注 0票数 2

我在我的C++项目中使用了C++,这是我在windows下创建的。在那里,依赖项是用vcpkg安装的(vcpkg.exe安装boost-json)。现在我想把这个项目移植到Ubuntu。但是我不知道如何在Linux下安装这个库。对于一个C++老手来说,这可能是显而易见的,但我无法让它开始工作。我在git项目或官方网站上找不到任何提示。

我已经试过了:

  • 使用CMake构建和安装库
  • 包括通过add_library在我的CMakeLists.txt中的代码
  • 仅通过复制包含文件夹将其用作标头。

在一个项目中包含这样一个库的最佳实践是什么,以及实现它的步骤是什么?是否有此类任务的教程?我最大的问题是,我不知道谷歌是为了什么。

我希望有人能帮我,谢谢你。

编辑:

正如@vre所提议的,我从源代码构建了boost 1.78.0。CMake现在发现了版本1.78.0的boost版本,并且包含错误已经消失。尽管如此,它仍然不能工作,因为Linux下的链接正在失败。我得到的输出如下:

代码语言:javascript
运行
复制
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `parse_server_config_json(std::filesystem::__cxx11::path)':
main.cpp:(.text+0x511): undefined reference to `boost::json::parse(boost::basic_string_view<char, std::char_traits<char> >, boost::json::storage_ptr, boost::json::parse_options const&)'
/usr/bin/ld: main.cpp:(.text+0x544): undefined reference to `boost::json::value::~value()'
/usr/bin/ld: main.cpp:(.text+0x589): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x5d0): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x615): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x662): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x6af): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o:main.cpp:(.text+0x758): more undefined references to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)' follow
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `parse_server_config_json(std::filesystem::__cxx11::path)':
main.cpp:(.text+0xb46): undefined reference to `boost::json::object::~object()'
/usr/bin/ld: main.cpp:(.text+0xbb4): undefined reference to `boost::json::value::~value()'
/usr/bin/ld: main.cpp:(.text+0xd4f): undefined reference to `boost::json::object::~object()'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::object::object(boost::json::object const&)':
main.cpp:(.text._ZN5boost4json6objectC2ERKS1_[_ZN5boost4json6objectC5ERKS1_]+0x4a): undefined reference to `boost::json::object::object(boost::json::object const&, boost::json::storage_ptr)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_object()':
main.cpp:(.text._ZN5boost4json5value9as_objectEv[_ZN5boost4json5value9as_objectEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_array()':
main.cpp:(.text._ZN5boost4json5value8as_arrayEv[_ZN5boost4json5value8as_arrayEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_string() const':
main.cpp:(.text._ZNK5boost4json5value9as_stringEv[_ZNK5boost4json5value9as_stringEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_int64()':
main.cpp:(.text._ZN5boost4json5value8as_int64Ev[_ZN5boost4json5value8as_int64Ev]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Server.dir/build.make:102: Server] Error 1
make[1]: *** [CMakeFiles/Makefile2:140: CMakeFiles/Server.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

我还添加了@GP8中提到的内容:

代码语言:javascript
运行
复制
find_package( 
 Boost 1.78 REQUIRED 
 COMPONENTS json 
)

Edit2:

我忘了链接boost-json。在我的CMakeLists.txt中添加了以下内容之后,构建在linux下是成功的:

代码语言:javascript
运行
复制
target_link_libraries(${PROJECT_NAME}
    Boost::boost
    Boost::json
)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-14 16:24:02

您应该首先使用以下命令安装boost:sudo apt-get install libboost-all-dev

要获得项目中包含的Boost库,您必须这样找到包:

代码语言:javascript
运行
复制
find_package( 
 Boost 1.65 REQUIRED 
 COMPONENTS  json 
)

然后,告诉CMake创建可执行文件的文件以及链接到哪个库:

代码语言:javascript
运行
复制
add_execublable( anyExecutable main.cpp )
target_link_libraries( exeLINK_PUBLIC ${Boost_LIBRARIES})
票数 3
EN

Stack Overflow用户

发布于 2022-03-14 22:02:49

@GPB概述了一般程序。

如果CMake/FindBoost还不支持Boost Json,那么最简单的方法就是

代码语言:javascript
运行
复制
#include <boost/json/src.hpp>

在确切的1(1)翻译单位,参与您的链接二进制。

请参阅只标头

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71470149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档