简体   繁体   中英

Using boost::json static library with cmake

I have this 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 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.~~

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

 #include <boost/json/src.hpp>

in one of your translation units. This makes Boost JSON effectively header-only.

Relevant documentation: https://github.com/boostorg/json#header-only

Next up, you can use this method to create your own static library. Keep in mind to optionally define visibility macros if your toolchain requires them (MSVC):

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: Add check for json component header in Boost 1.75+

The previous commit added support for 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. Unless you use 1_76 or 1_75 I suppose.

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. 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. Dude! Sweet!

So all we need is the latest cmake and we should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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