0

I'm trying to build a program that uses nlohmann::json but I'm having an issue with the includes. As per the instructions I'm using find_package(nlohmann_json 3.9.0 REQUIRED) and CMake will find the package but when I build I get an error telling me: fatal error: nlohmann\json.hpp: No such file or directory

This is the output of CMake:

Found nlohmann_json: /usr/local/lib64/cmake/nlohmann_json/nlohmann_jsonConfig.cmake (found suitable version "3.9.1", minimum required is "3.9.0")
-- Configuring done
-- Generating done
-- Build files have been written to: /root/vts/build

And here is my full CMake file

cmake_minimum_required(VERSION 3.10)

set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)

project(vitanza-service CXX)

add_subdirectory(VitanzaService)
add_executable(vts ${vts_SRC})

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

set_target_properties(vts PROPERTIES CXX_STANDARD 14)
set_target_properties(vts PROPERTIES CXX_STANDARD_REQUIRED ON)


LINK_DIRECTORIES(/usr/local/lib)


if (NOT WIN32)
    add_compile_options(-Wall -Werror -pipe -fvisibility=hidden)
endif ()

set(CMAKE_CXX_FLAGS_PERFORMANCE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")

if (CMAKE_COMPILER_IS_GNUCXX)
    add_compile_options(-fno-strict-aliasing)
endif ()

find_package(AWSSDK REQUIRED COMPONENTS dynamodb)
find_package(MySQL REQUIRED)
find_package(Boost 1.53.0 REQUIRED COMPONENTS system)
find_package(nlohmann_json 3.9.0 REQUIRED)


include_directories(${Boost_INCLUDE_DIRS} ${MYSQL_INCLUDE_DIR})

target_link_libraries(vts PRIVATE
        Boost::system
        ${AWSSDK_LINK_LIBRARIES}
        ${MYSQL_CLIENT_LIBS}
        nlohmann_json::nlohmann_json
        served libserved.so
        )

I'm not sure what the issue is. Am I supposed to add another variable in include_directories? I even tried adding the path to the include directory but that did not work either.

I also tried using add_path and adding that path to include_directories:

find_path(JSON_INCLUDE_DIR nlohmann/json.hpp
/usr/local/include)
...
include_directories(${JSON_INCLUDE_DIR})

Finally I tried using FetchContent as outlined here, but nothing seems to work.

I'm on CentOS 8 using GCC 8 and the latest version of nlohmann::json(I built it from source, including make install). Just tried it in Debian 10 and I have the same issue.

Thanks!

dominique120
  • 1,170
  • 4
  • 18
  • 31
  • Try using `make VERBOSE=1` to get the compiler commands and see what include path (if any) is being added for nlohman – Alan Birtles Oct 25 '20 at 03:46
  • You could search the cache file for cache varibables related to the lib (`CMakeCache.txt`). The fact that `target_link_libraries` does not provide you with access to the necessary headers indicates one of 2 things: Either the package does not provide info about its include dirs or it wants you to use a differnent include path, e.g. `#include "json.hpp"`. – fabian Oct 25 '20 at 06:15
  • 4
    Just noticed one thing: There is a backslash in the include path mentioned in the error output. You almost certainly should replace this backslash in the include instruction with a slash. Linux doesn't see backslashes as path separators... – fabian Oct 25 '20 at 06:23
  • @fabian you are absolutely right! This was the problem. I cant believe I've been fighting with this for two days. Thanks! Write an answer to this question so hat I can accept it as an answer :) – dominique120 Oct 25 '20 at 14:27

0 Answers0