c++ - 如何在 Linux 上使用 Boost/Json

标签 c++ boost cmake c++20 boost-json

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

我已经试过了:

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

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

我希望有人能帮助我,在此先感谢你。

编辑:

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

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

我忘了链接 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 使用哪个文件来创建您的可执行文件以及要链接哪些库:

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

关于c++ - 如何在 Linux 上使用 Boost/Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71470149/

相关文章:

c++ - 类的继承变量和自身变量的区别

c++ - 2 种模板相关类型名称的区别

c++ - 在 C/C++ 中签名扩展数字的最佳方法是什么

c++ - SDL 按键

c++ - boost::xpressive 查看序列的开头

c++ - 使用迭代器访问数据。 boost 图

macos - cmake、gcc、cuda 和 -m32

c++ - 初始化 asio::ip::address_v6() 的最快方法?

cmake add_library,然后是安装库目标

c++ - 交叉编译时CMake GTest fatal error (缺少stdlib.h)