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

Additional integration options #237

Closed
robertmrk opened this issue Apr 25, 2016 · 3 comments
Closed

Additional integration options #237

robertmrk opened this issue Apr 25, 2016 · 3 comments

Comments

@robertmrk
Copy link
Contributor

At the moment the only way to integrate the library into a client project is to add the json.hpp file to the source tree of the project. In some use cases that might not be the ideal solution.
For instance I'm working on a JSON processing library that uses the nlohmann/json library not only internally but in it's API as well. So client applications which would wan't to use the library would either have to use the json.hpp file supplied by the JSON processing library, which is awkward, or if they are already using the json.hpp file they can create and pass json variables to the library which can cause unpredictable problems if the version of the json.hpp file used by the client application and the JSON processing library differs.
Fortunately, CMake has a solution to the above problems.
With a few lines of CMake code we can create an interface library (a library target that doesn't generates any output, but it can be used to store usage requirements, which in our case would be the include directory of the json.hpp file) and export it. This way by including the nlohmann/json library into the client application with add_subdirectory, all targets would be able to find and use the library with the find_package command.

find_package(json)
add_executable(json-client ...) #or add_library(json-client ...)
target_link_libraries(json-client json)

With another few lines of CMake we can add an install target to the nlohmann/json project. Users would be able to install it using make install and use it, again, with the find_package command without including the json.hpp file into the project.
So by using find_package users can be sure that all the targets that depend on nlohmann/json are using the exact same file, and since we can optionally specify a version number for the find_package command too, users can avoid problems that might arise when using different versions.
In the end, we wouldn't lose anything. Users can continue to copy the json.hpp file into their projects if they feel that this is the right approach for integrating nlohmann/json into their projects. But we would gain some additional integration options.

@mmchen
Copy link

mmchen commented Feb 10, 2017

Does find_package(json) work already? I am using version 2.0.10, but find_package doesn't quite work. Thanks

@nlohmann
Copy link
Owner

@mmchen I have no idea since I'm no expert in Cmake. I merged PR #238, but I have no idea what this means for find_package.

@robertmrk
Copy link
Contributor Author

@mmchen It should work. But the package's name was changed sometimes after this PR got merged to nlohmann_json, so you should try to use find_package(nlohmann_json).

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

3 participants