So I have a project structure like this
lib
CMakeLists.txt
Game
include
game.h
src
game.cpp
CMakeLists.txt
Interpreter
include
InterpretJson.h
src
InterpretJson.cpp
CMakeLists.txt
tools
main.cpp
CMakeLists.txt
CMakeLists.txt
I am using cmake and in lib/interpreter/CMakeLists.txt I used
find_package(nlohmann_json 3.2.0 REQUIRED)
add_library(interpreter
src/InterpretJson.cpp
)
target_link_libraries(interpreter
PRIVATE
nlohmann_json::nlohmann_json
)
I have #include <nlohmann/json.hpp>
in InterpretJson.h
and also in game.h
Further, in lib/game/CMakeLists.txt
I have linked my interpreter library to my game library using
add_library(game src/game.cpp)
target_link_libraries(game
PRIVATE
interpreter
)
Running the command cmake
works properly and it says that its found the package in /opt/homebrew/Cellar
. The problem occurs when I run make in the build directory.
When I run make, it compiles and works fine on my linux VM however when using it on my M1 arm64 Mac, it gives the error :
In file included from /path/to/lib/game/src/game.cpp:1:
/path/to/lib/game/include/game.h:9:10: fatal error: 'nlohmann/json.hpp' file not found
#include <nlohmann/json.hpp>
^~~~~~~~~~~~~~~~~~~
I can fix this by using find_package(nlohmann...) and link_libraries(game nlohmann...)
AGAIN in lib/game/CMakeLists.txt
but this seems redundant. If I want to include the header in different directories, I don't want to find the package every time. Moreover, on a linux VM, I only need to find it once in lib/interpreter
and it gives no error, which is what I expect.
I also tried changing c_cpp_properties.json in .vscode to add the full path to "json.hpp" but this did not work either
"includePath": [
"${workspaceFolder}/**",
"/opt/homebrew/Cellar/nlohmann-json/3.10.5/include/"
]
So why is this happening and how do I make it so that I only have to find it once and use the header in any directory that links with interpreter (like I can in linux)?