Search code examples
c++visual-studioboost

Is there some bug with building Boost.JSON as header-only using Visual Studio?


I'm using Visual Studio 2022 v17.10.0 and Boost 1.84.0. The Boost.JSON documentation says the following:

To use as header-only; that is, to eliminate the requirement to link a program to a static or dynamic Boost.JSON library, simply place the following line in exactly one new or existing source file in your project.

#include <boost/json/src.hpp>

MSVC users must also define the macro BOOST_JSON_NO_LIB to disable auto-linking.

I do the above and get the following linker error:

fatal error LNK1104: cannot open file 'libboost_container-vc143-mt-x64-1_84.lib'

Minimal reproducible example below:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)

project(min_rep_example VERSION 1.0.0 LANGUAGES CXX)

find_package(Boost 1.80 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS}) 

add_executable(min_rep_example
    main.cpp
)

main.cpp:

#include <string>
#define BOOST_JSON_NO_LIB 
#include <boost/json/src.hpp>

int main() {
    std::string json_str = "{\"foo\": 42, \"bar\" : \"quux\"}";
    auto json = boost::json::parse(json_str);
    return 0;
}

Solution

  • Boost Json has a dependency on container, which means that boost is trying to auto include libboost_container-vc143-mt-x64-1_84.lib

    The solution is to ensure that you also turn off auto-linking for container, by adding the following to your main.cpp:

    #define BOOST_CONTAINER_NO_LIB