6

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 
4
  • Docs don't mention static library build variant. Commented Apr 18, 2021 at 8:45
  • @user7860670 It said it can be Link to a built static or dynamic Boost library, and I indeed found libboost_json-mt.a Commented Apr 18, 2021 at 8:51
  • It says that your executable needs to link to a built static or dynamic Boost library when using boost.json along with other boost libraries, not that boost.json itself comes in static flavor. Commented Apr 18, 2021 at 8:56
  • Not sure what that means. It seems to work using target_link_libraries(<Target> PRIVATE boost_json-mt) too, so it definitely can be statically linked. Would be great if you can answer with the "intended" usage of CMakeLists.txt Commented Apr 18, 2021 at 9:06

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I understand this part. But the package indeed comes with libboost_json-mt.a file, why it is not found by cmake in the find_package call?
It looks like boost::json hasn't been added as a component as of 3.21. There's mention of bzip2 and zlib components being added as windows only in 3.9, and for all platforms in 3.19, but no other components are mentioned At All. I suspect it's just missing.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.