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

Usage of the jsoncpp CMake package #455

Closed
ghisvail opened this issue Mar 27, 2016 · 14 comments
Closed

Usage of the jsoncpp CMake package #455

ghisvail opened this issue Mar 27, 2016 · 14 comments
Labels
build or testing cmake, meson, continuous integration, or testing related help wanted

Comments

@ghisvail
Copy link

Dear jsoncpp developers,

I tried to use the CMake package built by jsoncpp (and provided by libjsoncpp-dev on Debian / Ubuntu) on a CMake project depending on it.

I tried the following discovery via CMake config mode:

find_package(jsoncpp REQUIRED)
include_directories(${jsoncpp_INCLUDE_DIRS})
link_libraries(${jsoncpp_LIBRARIES})

But it does not seem to work. I then ran the the following test:

cmake_minimum_required(VERSION 2.6.2)
project(jsoncpp_test)

find_package(jsoncpp REQUIRED)
message("FOUND CONFIG: ${jsoncpp_CONFIG}")
message("INCLUDE DIRS: ${jsoncpp_INCLUDE_DIRS}")
message("LIBRARIES: ${jsoncpp_LIBRARIES}")

which produced the following output:

FOUND CONFIG: /usr/lib/x86_64-linux-gnu/cmake/jsoncpp/jsoncppConfig.cmake
INCLUDE DIRS: 
LIBRARIES: 

meaning the CMake package file is found but none of the expected variables is set.

How are we expected to discover and use jsoncpp under CMake then? FYI, using pkg-config works fine, but is not what I would consider a solution.

Many thanks,
Ghis

@arrufat
Copy link

arrufat commented Mar 3, 2017

I've managed to make it work like this

find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)
link_libraries(${JSONCPP_LIBRARIES})
add_executable(myprogram myprogram.cpp)
target_link_libraries(myprogram ${JSONCPP_LIBRARIES})

@cdunn2001
Copy link
Contributor

Thanks, Max Headroom. Update the wiki too, if you want.

@pazz
Copy link

pazz commented Oct 26, 2017

thanks @arrufat
Silly question: you would then include jsoncpp in your main file as

#include <jsoncpp/json/json.h>

right? Does that mean the code is not portable in the sense that if will only compile if run on a distri that packages the headers in this way?

@arrufat
Copy link

arrufat commented Oct 26, 2017

Yes, I had exactly this issue with Arch Linux and Ubuntu...

@pazz
Copy link

pazz commented Oct 26, 2017

Thanks for the quick response. I've just seen that you can add the discovered include path with

include_directories("${JSONCPP_INCLUDE_DIRS}")

in CMakeLists.txt. Then you can just include as <json/json.h>...

@cdunn2001 cdunn2001 added the build or testing cmake, meson, continuous integration, or testing related label Nov 16, 2017
@christian-rauch
Copy link

christian-rauch commented Mar 27, 2019

The jsoncppConfig.cmake defines property INTERFACE_INCLUDE_DIRECTORIES for targets jsoncpp_lib and jsoncpp_lib_static.

You need to query the target property and set it manually:

get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

Linking is done via:

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)

@cdunn2001
Copy link
Contributor

@christian-rauch , If you submit a PR, we'll merge it. We don't maintain the Cmake files anymore; users do that. We use only the Meson files ourselves.

@christian-rauch
Copy link

@cdunn2001 The code snippet that I mentioned has to be used on the user side, e.g. an application that wants to use jsoncpp.

If someone is interested in making the usage of jsoncpp in CMake projects easier, there is a way to export header and libraries in such a way that an application can just import the library without dealing with include directories and libraries manually: https://rix0r.nl/blog/2015/08/13/cmake-guide/

@cdunn2001
Copy link
Contributor

Thanks. Updated our Wiki.

If you submit the change to simplify Cmake, we'd merge it.

@mathemaphysics
Copy link

I'm trying to use jsoncpp via add_subdirectory(). I can't seem to guess the target names right. Neither jsoncpp nor json appear to be library targets. I'm planning on reading through all the CMake files, but I thought I'd ask here just in case anyone could help.

@devkarlos
Copy link

I'm trying to use jsoncpp via add_subdirectory(). I can't seem to guess the target names right. Neither jsoncpp nor json appear to be library targets. I'm planning on reading through all the CMake files, but I thought I'd ask here just in case anyone could help.

Did you figured it out ?

@Leonkoithara
Copy link

Hi I'm using mac os x(installed jsoncpp using brew install jsoncpp) and I found ${JSONCPP_LINK_LIBRARIES} was the required library to be linked. Sample CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
set(CMAKE_CXX_STANDARD 11)

find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)

add_executable(utils main.cpp)

target_link_libraries(
    utils
    ${JSONCPP_LINK_LIBRARIES}
)

@Lithimlin
Copy link

Lithimlin commented Aug 1, 2023

I can't work out how to include this library when installed on Ubuntu 22.04.

I've tried the version in the wiki, the one above, and a method I found on stackoverflow.
I'm trying to include the library using #include <json/json.h>. However, when using #include <jsoncpp/json/json.h>, it works

@eli-schwartz
Copy link

eli-schwartz commented Aug 9, 2023

Hi I'm using mac os x(installed jsoncpp using brew install jsoncpp) and I found ${JSONCPP_LINK_LIBRARIES} was the required library to be linked. Sample CMakeLists.txt

From the CMake docs: https://cmake.org/cmake/help/latest/module/FindPkgConfig.html#command:pkg_check_modules

New in version 3.6: The IMPORTED_TARGET argument will create an imported target named PkgConfig::<prefix> that can be passed directly as an argument to target_link_libraries().

Please switch this:

pkg_check_modules(JSONCPP jsoncpp)

target_link_libraries(
    utils
    ${JSONCPP_LINK_LIBRARIES}
)

for this:

pkg_check_modules(JSONCPP jsoncpp IMPORTED_TARGET GLOBAL)

target_link_libraries(
    utils
    PkgConfig::JSONCPP
)

prince-chrismc added a commit to cjserio/jwt-cpp that referenced this issue Dec 9, 2023
"they" do not maintain the CMake scripts so this will be fun open-source-parsers/jsoncpp#455 (comment)
prince-chrismc added a commit to Thalhammer/jwt-cpp that referenced this issue Dec 16, 2023
* Implemented JsonCPP Traits

* quick pass to add CI

* correct find_packge to use config and not be required

* typo in default version

* jsoncpp forces out of source builds

* disable tests and shared library

* add missing build step

* try a different target + build less 

"they" do not maintain the CMake scripts so this will be fun open-source-parsers/jsoncpp#455 (comment)

* fix name for traits (missed replaces)

* Added missing array_type constructor so that basic_claims can be formed from sets of values

* Linters and more tests + fetchcontent gtest

* linter

* support library names with many separators

* linters

* run clang format after rendering defaults

---------

Co-authored-by: Chris Mc <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build or testing cmake, meson, continuous integration, or testing related help wanted
Projects
None yet
Development

No branches or pull requests

10 participants