简体   繁体   English

使用 boost::json static 库和 cmake

[英]Using boost::json static library with cmake

I have this CMakeLists.txt我有这个CMakeLists.txt

set(Boost_USE_STATIC_LIBS   ON)
find_package(Boost REQUIRED COMPONENTS json)
include_directories(${BOOST_INCLUDE_DIRS})
target_link_libraries(<Target> PRIVATE Boost::json)

But I get cmake warnings但我收到 cmake 警告

[cmake] CMake Warning at C:/Program Files/CMake/share/cmake3.20/Modules/FindBoost.cmake:2185 (message):
[cmake]   No header defined for json; skipping header check (note: header-only
[cmake]   libraries have no designated component)

The project can configure and build tho.该项目可以配置和构建寿。 ~~But the compile_command.json I found didn't actually link to the static library, which clearly indicate it is not found and was compiled as header only library.~~ ~~但是我发现的compile_command.json实际上并没有链接到static库,这清楚地表明它没有找到并且只编译为header库~~

EDIT: I found it actually links to libboost_json-mt.a in the generated build.ninja file, so it is weird the fact that cmake does not find the static library in the find_package call.编辑:我发现它实际上链接到生成的build.ninja文件中的libboost_json-mt.a ,所以奇怪的是 cmake 在find_package调用中找不到 static 库。

build MyTest.exe MyTest[1]_tests.cmake: CXX_EXECUTABLE_LINKER__MyTest_Debug CMakeFiles/MyTest.dir/test.cpp.obj CMakeFiles/MyTest.dir/BasicStructures/Uri.cpp.obj | C$:/msys64/mingw64/lib/libgtest_main.dll.a C$:/msys64/mingw64/lib/libboost_json-mt.a C$:/msys64/mingw64/lib/libgtest.dll.a C$:/msys64/mingw64/lib/libboost_container-mt.a
  FLAGS = -g
  LINK_LIBRARIES = C:/msys64/mingw64/lib/libgtest_main.dll.a  C:/msys64/mingw64/lib/libboost_json-mt.a  C:/msys64/mingw64/lib/libgtest.dll.a  C:/msys64/mingw64/lib/libboost_container-mt.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
  OBJECT_DIR = CMakeFiles\MyTest.dir
  POST_BUILD = cmd.exe /C "cd /D C:\Users\Peter\Desktop\LspCpp\build && "C:\Program Files\CMake\bin\cmake.exe" -D TEST_TARGET=MyTest -D TEST_EXECUTABLE=C:/Users/Peter/Desktop/LspCpp/build/MyTest.exe -D TEST_EXECUTOR= -D TEST_WORKING_DIR=C:/Users/Peter/Desktop/LspCpp/build -D TEST_EXTRA_ARGS= -D TEST_PROPERTIES= -D TEST_PREFIX= -D TEST_SUFFIX= -D NO_PRETTY_TYPES=FALSE -D NO_PRETTY_VALUES=FALSE -D TEST_LIST=MyTest_TESTS -D CTEST_FILE=C:/Users/Peter/Desktop/LspCpp/build/MyTest[1]_tests.cmake -D TEST_DISCOVERY_TIMEOUT=5 -D TEST_XML_OUTPUT_DIR= -P "C:/Program Files/CMake/share/cmake-3.20/Modules/GoogleTestAddTests.cmake""
  PRE_LINK = cd .
  RESTAT = 1
  TARGET_FILE = MyTest.exe
  TARGET_IMPLIB = libMyTest.dll.a
  TARGET_PDB = MyTest.exe.dbg

Instead of the static link option, I think you're supposed to include而不是 static 链接选项,我认为你应该包括

 #include <boost/json/src.hpp>

in one of your translation units.在您的一个翻译单元中。 This makes Boost JSON effectively header-only.这使得 Boost JSON 有效地只有标头。

Relevant documentation: https://github.com/boostorg/json#header-only相关文档: https://github.com/boostorg/json#header-only

Next up, you can use this method to create your own static library.接下来,您可以使用此方法创建自己的 static 库。 Keep in mind to optionally define visibility macros if your toolchain requires them (MSVC):如果您的工具链需要它们(MSVC),请记住选择性地定义可见性宏:

https://github.com/boostorg/json#standalone-shared-library https://github.com/boostorg/json#standalone-shared-library

The latest commit to the findboost package at https://gitlab.kitware.com/cmake/cmake/-/commits/master/Modules/FindBoost.cmake dated June 4 describes itself thusly:对 findboost package 在https://gitlab.kitware.com/cmake/cmake/-/commits/master/Modules/FindBoost.Z272CEADB8458A65015B2AEZ4B56

FindBoost: Add check for json component header in Boost 1.75+

The previous commit added support for boost 1_76.之前的提交增加了对 boost 1_76 的支持。 As of this writing the latest is 1_77_0... so if you use 1_77 you'll get a bunch (one per component?) of warning messages about unsupported versions.在撰写本文时,最新的是 1_77_0... 所以如果您使用 1_77,您将收到一堆(每个组件一个?)关于不受支持版本的警告消息。 Unless you use 1_76 or 1_75 I suppose.除非你使用 1_76 或 1_75 我想。

So the support you want is available now, but was not when you wrote this question.所以你想要的支持现在可用,但在你写这个问题时还没有。 I don't know if the currently available version of cmake (3.21.2) includes that update or not.我不知道当前可用的 cmake (3.21.2) 版本是否包含该更新。 The docs certainly don't mention it... but incomplete docs are hardly a new thing under the sun.文档当然没有提到它......但不完整的文档在阳光下并不是什么新鲜事。

So the support we need is out there now/finally, and according to the gitlab site, that change was included in cmake 3.21.2.所以我们需要的支持现在/终于出来了,根据 gitlab 站点,该更改包含在 cmake 3.21.2 中。 Dude!老兄! Sweet!甜的!

So all we need is the latest cmake and we should be good to go.所以我们只需要最新的 cmake,我们应该对 go 很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM