如何在 linux 上使用 Boost/Json

How to use Boost/Json on linux

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

我已经试过了:

在项目中包含此类库的最佳做法是什么?实现它的步骤是什么?是否有此类任务的教程?我最大的问题是,我不知道要 google 做什么。

希望有人能帮帮我,先谢谢了。

编辑:

按照@vre 的建议,我从源代码构建了 boost 1.78.0。 CMake 现在找到版本为 1.78.0 的提升版本并且包含错​​误消失了。尽管如此,它仍然无法正常工作,因为 Linux 下的 linking 失败了。我得到以下输出:

/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 提到的:

find_package( 
 Boost 1.78 REQUIRED 
 COMPONENTS json 
)

编辑2:

我忘了 link boost-json。将以下内容添加到我的 CMakeLists.txt 后,构建在 linux 下成功:

target_link_libraries(${PROJECT_NAME}
    Boost::boost
    Boost::json
)

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

要将 Boost 库包含在您的项目中,您必须以这种方式找到包:

find_package( 
 Boost 1.65 REQUIRED 
 COMPONENTS  json 
)

然后,您可以告诉 CMake 使用哪个文件创建可执行文件以及针对哪个库 link :

add_execublable( anyExecutable main.cpp )
target_link_libraries( exeLINK_PUBLIC ${Boost_LIBRARIES})

@GPB 概述了一般程序。

如果您的 CMake/FindBoost 还不支持 Boost Json,最简单的做法是

#include <boost/json/src.hpp>

正好有 1(一)个翻译单元参与您的链接二进制文件。

Header Only