skip to Main Content

I am a newb in cmake and building external libraries.

I am trying to add nlohmann_json library to my project but I don’t want to include the entire repository in my project files, I saw in the readme file that I only need the json.hpp file to use in my project; however this file includes multiple other files from the repo.

How do I add this single file and use it instead of adding the entire library and how do I integrate this change into my CMakeLists.txt

set(nlohmann_json_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build/lib/json)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(nlohmann_json 3.2.0 REQUIRED)

# Some Other Configurations

target_link_libraries(PROJECT_NAME PRIVATE Qt${QT_VERSION_MAJOR}::Widgets nlohmann_json::nlohmann_json)

This was how I integrated the library when I cloned the entire repo

2

Answers


  1. You should grab the amalgamated file from the single_include directory or the latest release.

    Login or Signup to reply.
  2. You can use the single include header file.. Now there are two ways to get this header file. One way would be directly downloading the file from the location and using it in your project. Another way would be to clone the entire repo and copy the json.hpp header file. Your cmake should look something like below.

        cmake_minimum_required(VERSION 3.10)
        project(tmp)
        add_executable(${PROJECT_NAME} tmp.cpp json.hpp)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search