Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to locate nlohmann_jsonConfig.cmake #1755

Closed
spmcv opened this issue Sep 18, 2019 · 14 comments
Closed

Unable to locate nlohmann_jsonConfig.cmake #1755

spmcv opened this issue Sep 18, 2019 · 14 comments

Comments

@spmcv
Copy link

spmcv commented Sep 18, 2019

I've added the nlohmann/json as a submodule (default branch) to my repo at external/json. I then used this CMakeLists example to link to my shared library:

cmake_minimum_required(VERSION 3.14)
project(foo VERSION 0.1.0 LANGUAGES CXX)
add_library(${CMAKE_PROJECT_NAME} SHARED Main.cpp)

# ...

find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
    # Update submodules as needed
    option(GIT_SUBMODULE "Check submodules during build" ON)
    if(GIT_SUBMODULE)
        message(STATUS "Submodule update")
        execute_process(
                COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                RESULT_VARIABLE GIT_SUBMODULE_RESULT
        )
        if(NOT GIT_SUBMODULE_RESULT EQUAL "0")
            message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMODULE_RESULT}, please checkout submodules")
        endif()
    endif()
endif()

add_subdirectory("${PROJECT_SOURCE_DIR}/external/json")
find_package(nlohmann_json 3.2.0 REQUIRED)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE nlohmann_json::nlohmann_json)

I get this error while loading CMake:

CMake Error at CMakeLists.txt:122 (find_package):
  By not providing "Findnlohmann_json.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "nlohmann_json", but CMake did not find one.

  Could not find a package configuration file provided by "nlohmann_json"
  (requested version 3.2.0) with any of the following names:

    nlohmann_jsonConfig.cmake
    nlohmann_json-config.cmake

  Add the installation prefix of "nlohmann_json" to CMAKE_PREFIX_PATH or set
  "nlohmann_json_DIR" to a directory containing one of the above files.  If
  "nlohmann_json" provides a separate development package or SDK, be sure it
  has been installed.

However I see that nlohmann_jsonConfig.cmake exists at cmake-build-debug/external/json/nlohmann_jsonConfig.cmake. I'm using the default CMake configuration
in CLion 2019.2.2 (debug) that uses CMake 3.14.5 on macOS 10.14.6 (18G95). Is addsubdirectory is insufficient, or is there a bug somewhere else? I don't see a significant difference with the add subdirectory example.

@spmcv spmcv closed this as completed Sep 19, 2019
@spmcv
Copy link
Author

spmcv commented Sep 19, 2019

Sorry, new to CMake; the "combined " method on the README makes a lot more sense

@guiyuliu
Copy link

Sorry, new to CMake; the "combined " method on the README makes a lot more sense
how to fix this problem??
where is nlohmann_jsonConfig.cmake

@guiyuliu
Copy link

do we need to install?
this is not nice to new users

@nlohmann
Copy link
Owner

do we need to install?
this is not nice to new users

?

@guiyuliu
Copy link

do we need to install?
this is not nice to new users

?

how to fix this issue ? when I use it in cmake , I find the same problem
cmake can only find a package when there is a “FindXXX.cmake” file in “/usr/share/cmake/Modules”
After I download this repo, how to let cmake can find it ?

@nlohmann
Copy link
Owner

Yes, you need to install the package.

@k-styles
Copy link

Hi, I'm still a bit unclear on where to find nlohmann_jsonConfig.cmake.
I have seen the docs saying:

The package configuration file, nlohmann_jsonConfig.cmake, can be used either from an install tree or directly out of the build tree.

But don't really know what to make out of it, as I'm still quite new to software development. I would really appreciate any kind of help.

@falbrechtskirchinger
Copy link
Contributor

https://json.nlohmann.me/integration/

Please start there and then come back with concrete questions if you have any. I've directed you to this part of the documentation before.

@k-styles
Copy link

k-styles commented Jun 21, 2022

@falbrechtskirchinger I have checked out the docs before, but couldn't make out what

The package configuration file, nlohmann_jsonConfig.cmake, can be used either from an install tree or directly out of the build tree.

is saying. Where should I look for the .cmake file? or rephrased: What's meant by install tree and build tree? I hope I was clear.

@falbrechtskirchinger
Copy link
Contributor

What's meant by install tree and build tree?

The build tree is the directory you invoked cmake in or the directory specified via the -B option.
The install tree refers to the contents of the directory specified via -DCMAKE_INSTALL_PREFIX= after building the install target, i.e., make install.

If you're inexperienced, there're easier options to integrate the library. For example, by just including the single-header json.hpp from your source file(s), or, if you really want to use CMake, include the whole project via add_subdirectory().

@k-styles
Copy link

Thanks @falbrechtskirchinger

@nlohmann
Copy link
Owner

If you have any proposal how to improve documentation, please let me know.

@k-styles
Copy link

Hi @nlohmann ! I think something along the lines:

The package configuration file, nlohmann_jsonConfig.cmake, could be found inside the directory where you generated MakeFile using CMake ...

can be added. Basically, something like providing a description of build tree along with it, as like me many novice software developers might have a poor "software vocabulary" and might waste their time wondering and googling: "what is a build tree". Providing such a small description might prove to be convenient for them and might help with adding this new term to their dictionary.

@andzsinszan
Copy link

andzsinszan commented Dec 4, 2023

If you have any proposal how to improve documentation, please let me know.

I suggest explicitly stating that find_package works only if nlohmann_json is installed. The term external is a very indirect implication for system-wide installation.

In general, the CMake/External and CMake/Embedded sections are difficult to understand for newcomers.
These sections are about:

  1. using nlohmann_json when the library is installed system-wide (=External)
  2. using nlohmann_json without installation (=Embedded) ; e.g. repo was just git-cloned, not make/install-ed
    • if make-d (not installed), cmake variable nlohmann_json_DIR should be set

I general, I would avoid using the word Embedded as in the context of C++ it is associated with development for embedded devices. The section has nothing to do with embedded devices, it is about setting up nlohmann_json within a project tree (without installing it).

The package configuration file, nlohmann_jsonConfig.cmake, could be found

I don't see how this helps. Normally, users should not be aware of package config files.

As a further improvement, I would add some notes on installation.
I see no reason why not to mention the debian package nlohmann-json3-dev.

  sudo apt-get install nlohmann-json3-dev

Also, RPM, brew packages...

(edit: corrected info about nlohmann_json_DIR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants