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

Integrating into existing CMake Project #1573

Closed
jeffmorr opened this issue Apr 20, 2019 · 11 comments
Closed

Integrating into existing CMake Project #1573

jeffmorr opened this issue Apr 20, 2019 · 11 comments

Comments

@jeffmorr
Copy link

Hi all,

I am new to using CMake and am very confused as to how I am supposed to integrate this project into my existing CMake Project. This is what I currently have:

#cmake version to be used
cmake_minimum_required(VERSION 3.5.1)

#project name
project("Json Library")

set(JSON_BuildTests OFF CACHE INTERNAL "")

#flags
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(WINDOWS_COMPILE_FLAGS "/W3 /D_CRT_SECURE_NO_WARNINGS -D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING=1")
set(COMPILE_FLAGS "${WINDOWS_COMPILE_FLAGS}")
SET(${flags} "${COMPILE_FLAGS} ")
set(CMAKE_PREFIX_PATH "$CMAKE_SOURCE_DIR}/third-party/nlohmann;${CMAKE_PREFIX_PATH}")
find_package(nlohmann_json 3.6.1 REQUIRED)
add_library(foo ...)
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)

add_executable(Driver foo ${CMAKE_SOURCE_DIR}/src/LootGeneration.cpp)

The result of this file creates this error and I'm not sure how to fix:

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.6.1) 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.

Any help is appreciated! Thank you.

@jeffmorr
Copy link
Author

Thank you!

@coderDec
Copy link

coderDec commented Mar 7, 2020

@jeffmorr how did you fix that error?
I tried to build the project in cmake_import directory and I still get the same error.

@alex-cornejo
Copy link

Same problem here. I have installed the library through vcpkg. This is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(test_cpp)
set(CMAKE_CXX_STANDARD 14)
find_package(nlohmann-json REQUIRED)
add_executable(test_cpp main.cpp)
target_link_libraries(test_cpp nlohmann_json)

I get the next output:

  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....

Can anybody help me about what it is wrong in my cmake file?

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

I'm not using vcpkg, but looking at https://vcpkg.readthedocs.io/en/latest/examples/installing-and-using-packages/ it seems you need to pass a toolchain file.

@alex-cornejo
Copy link

I'm not using vcpkg, but looking at https://vcpkg.readthedocs.io/en/latest/examples/installing-and-using-packages/ it seems you need to pass a toolchain file.

I am starting with c++ and I finished switching to conan. Now I am using the library and it is working fine. Thanks so much!!!

@CtfChan
Copy link

CtfChan commented Sep 3, 2020

Hey guys I had trouble with find_package as well. I am using Ubuntu 18.04.

In the end, I used this line and the package was found

find_package(nlohmann_json REQUIRED
    CONFIG
    PATHS "/usr/lib/cmake/"
)

@brentsony
Copy link

Set you Toolchain to:

$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake

(You'll need to specify $VCPKG_ROOT yourself.)

If you're using a Remote Toolchain in an IDE, you cannot just set the toolchain, so include the above vcpkg toolchain in your CMakeLists.txt file, instead:

include($ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)

@WillNilges
Copy link

Here are two example projects of how to integrate the library:

* https://github.com/nlohmann/json/blob/develop/test/cmake_import/project/CMakeLists.txt

* https://github.com/nlohmann/json/blob/develop/test/cmake_add_subdirectory/project/CMakeLists.txt

Looks like these tests have moved.
For the lazy:

@nlohmann
Copy link
Owner

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

No branches or pull requests

8 participants
@nlohmann @alex-cornejo @jeffmorr @brentsony @CtfChan @WillNilges @coderDec and others