0

I am trying to link the NLohmann json library to my json interpreter via cmake.

I keep getting the error : fatal error: 'nlohmann/json.hpp' file not found #include <nlohmann/json.hpp>

From the file :

#include <nlohmann/json.hpp>
#include <fstream>
#include "InterpretJson.h"
#include "game.h"
using namespace std;
using json = nlohmann::json;

InterpretJson(string path){
    this->path = path;
    ifstream f(path);
    json jData = json::parse(f);
    f.close();
    this->data = jData;
}

Game interpret(Game& game){}

The CMakeLists.txt in the directory which contains src/interpretJson.cpp and include/interpretJson.h.:

find_package(nlohmann_json 3.2.0 REQUIRED)

add_library(interpreter
    src/interpretJson.cpp
)


target_include_directories(interpreter
    PUBLIC
        $<INSTALL_INTERFACE:include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/src
        ${nlohmann_json_INCLUDE_DIR}
)

target_link_libraries(interpreter
    PRIVATE 
        ${nlohmann_json_LIBRARIES}
)

set_target_properties(interpreter
                    PROPERTIES
                    LINKER_LANGUAGE CXX
                    CXX_STANDARD 17
)

install(TARGETS interpreter
    ARCHIVE DESTINATION lib
)

How do I fix this?

Edit: This is issue is only happening on arm64 Mac M1 but it is working fine on a linux ubuntu VM. However, the vm is slow and I would still like to know how to make it work on Mac

callum arul
  • 159
  • 6
  • Does the compilation line have the proper include path? Build with verbose output and [edit] your question. – Stephen Newell Oct 18 '22 at 11:14
  • 1
    Have you checked [nlohmann_json documentation](https://github.com/nlohmann/json/blob/develop/README.md#cmake) about integrating it with a CMake project? The documentation notes linking with `nlohmann_json::nlohmann_json` **target** and doesn't describe **variables** like `nlohmann_json_INCLUDE_DIR` and `nlohmann_json_LIBRARIES` which are used in your code. – Tsyvarev Oct 18 '22 at 12:07
  • Ive tried using it the way the documentation describes it using nlohmann_json::nlohmann_json but it results in the same error. @StephenNewell I found the following information about the include path :CPPFLAGS: -I/opt/homebrew/opt/nlohmann_json/include. I am new to cmake so how do I add this in my cmake so the I don't have to add it the compilation line? – callum arul Oct 18 '22 at 23:30
  • Are the headers in that folder? – Stephen Newell Oct 19 '22 at 00:14
  • The headers are in /opt/homebrew/Cellar/nlohmann-json/3.10.5/include/nlohmann since I installed using homebrew. I also tried cloning it into my base directory and it still produces the same error – callum arul Oct 19 '22 at 00:37

1 Answers1

0

I have this in my CMakeLists.txt

FetchContent_Declare(
        nlohmann
        GIT_REPOSITORY  "https://github.com/onavratil-monetplus/json"
        GIT_TAG         "master"
)

FetchContent_MakeAvailable( nlohmann )
target_include_directories ( main PUBLIC ${nlohmann_json_SOURCE_DIR}/include )

and in src file ...

#include "nlohmann/json.hpp"
Richard
  • 302
  • 2
  • 9