0

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)?

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
callum arul
  • 159
  • 6
  • Does this only happen inside VS Code or also when running cmake on its own? – Ulrich Eckhardt Oct 28 '22 at 06:18
  • At a guess your Linux library is installed somewhere in the system paths but you're Mac one isn't. Linking to our everywhere it's used is the correct approach. You could move the `find_package` into a top level cmake file though – Alan Birtles Oct 28 '22 at 06:45
  • @UlrichEckhardt it happens when I run cmake on the Mac terminal. I just checked on vscode and it happens there as well. – callum arul Oct 28 '22 at 07:41
  • 1
    You **privately** (PRIVATE) link `interpreter` target with `nlohmann_json::nlohmann_json`. So that linkage affects **only** the `interpreter` target, but doesn't affect its *consumers*. Use PUBLIC for make linkage accessible both for the target and its consumers. – Tsyvarev Oct 28 '22 at 07:56
  • @Tsyvarev Yes! That was the problem. It works now, thanks! – callum arul Oct 28 '22 at 09:41
  • So it's not specific to VS Code, hence the tag can be remove. Please clarify the scope in advance, like extracting a [mcve]. It also helps you find the reason of the issue yourself. – Ulrich Eckhardt Oct 28 '22 at 17:21

0 Answers0