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

How to include in an external project #2083

Closed
hect1995 opened this issue May 3, 2020 · 7 comments
Closed

How to include in an external project #2083

hect1995 opened this issue May 3, 2020 · 7 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@hect1995
Copy link

hect1995 commented May 3, 2020

  • Describe what you want to achieve.
    I want to import the json functionality to my own project

  • Describe what you tried.
    Following the README, I have cloned this repo in an external folder not in the path of mine and in my CMake I do:

find_package(nlohmann_json 3.2.0 REQUIRED)
add_library(${BINARY}_lib nlohmann_json::nlohmann_json)
target_link_libraries(${BINARY}_lib PRIVATE nlohmann_json::nlohmann_json)

But get this output:

CMake Error at HackerRank/CMakeLists.txt:7 (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.

Sorry in advance for the question but would be much appreciated if you could help me.

  • Describe which system (OS, compiler) you are using.
    Mac OS Catalina and LLDB

  • Describe which version of the library you are using (release version, develop branch).
    I cloned the latest master

@nlohmann
Copy link
Owner

nlohmann commented May 3, 2020

Did you install the library? This is required if you want to find it via find_package.

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label May 3, 2020
@hect1995
Copy link
Author

hect1995 commented May 3, 2020

Yes installed but I have one doubt. Once it is installed with Homebrew, then in my CMake I need to add:

# CMakeLists.txt
find_package(nlohmann_json 3.2.0 REQUIRED)
...
add_library(foo ...)
...
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)

what is what goes in the ... inside add_library?

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label May 3, 2020
@nlohmann
Copy link
Owner

nlohmann commented May 3, 2020

I could do the following:

brew tap nlohmann/json
brew install nlohmann-json

and the following project:

cmake_minimum_required(VERSION 3.1)

project(DummyProject CXX)

find_package(nlohmann_json 3.2.0 REQUIRED)
add_executable(dummy dummy.cpp)
target_link_libraries(dummy PRIVATE nlohmann_json::nlohmann_json)
#include <nlohmann/json.hpp>
#include <iostream>

int main()
{
    std::cout << nlohmann::json::meta() << std::endl;
}

can be configured via:

$ cmake ..
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found nlohmann_json: /usr/local/lib/cmake/nlohmann_json/nlohmann_jsonConfig.cmake (found suitable version "3.7.3", minimum required is "3.2.0") 
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/niels/Desktop/foo/build

and build with

$  make
Scanning dependencies of target dummy
[ 50%] Building CXX object CMakeFiles/dummy.dir/dummy.cpp.o
[100%] Linking CXX executable dummy
[100%] Built target dummy
$ ./dummy
{"compiler":{"c++":"201103","family":"clang","version":"11.0.3 (clang-1103.0.32.59)"},"copyright":"(C) 2013-2017 Niels Lohmann","name":"JSON for Modern C++","platform":"apple","url":"https://github.com/nlohmann/json","version":{"major":3,"minor":7,"patch":3,"string":"3.7.3"}}

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 3, 2020
@hect1995
Copy link
Author

hect1995 commented May 3, 2020

You are right, thank @nlohmann . I know it does not concern you as it is a problem from my side but I have In my project one general CMake and one other inside the subproject. With this I get the error:
/Users/hectoresteban/Documents/C++/C++Projects/DailyCoding/HackerRank/read_json_file.cpp:1:10: fatal error: 'nlohmann/json.hpp' file not found
This is my CMake in the main directory:

cmake_minimum_required(VERSION 3.15)
project(DailyCoding)
set(CMAKE_CXX_STANDARD 17)
include_directories(HackerRank) # points where I build everything
find_package(nlohmann_json 3.2.0 REQUIRED)
add_subdirectory(HackerRank)

An this is the CMake in HackerRank where I include your package:

set(BINARY ${CMAKE_PROJECT_NAME})
set(SOURCES read_json_file.cpp)
add_executable(${BINARY}_run ${SOURCES})
add_library(${BINARY}_lib STATIC ${SOURCES})
target_link_libraries(${BINARY}_run PRIVATE nlohmann_json::nlohmann_json)

Do you know what is the reason why with this structure happens this error?

Thank a lot

@nlohmann
Copy link
Owner

nlohmann commented May 4, 2020

How does read_json_file.cpp:1:10 look like?

@hect1995
Copy link
Author

hect1995 commented May 4, 2020

#include <nlohmann/json.hpp>

@nlohmann
Copy link
Owner

nlohmann commented May 4, 2020

Are there errors or warning running CMake?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants