Skip to content

How to link jsoncpp library in cmake project #927

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

Closed
ParikaGoel opened this issue May 28, 2019 · 8 comments
Closed

How to link jsoncpp library in cmake project #927

ParikaGoel opened this issue May 28, 2019 · 8 comments

Comments

@ParikaGoel
Copy link

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)

Originally posted by @christian-rauch in #455 (comment)

@ParikaGoel ParikaGoel changed the title The jsoncppConfig.cmake defines property INTERFACE_INCLUDE_DIRECTORIES for targets jsoncpp_lib and jsoncpp_lib_static. How to link jsoncpp library in cmake project May 28, 2019
@ParikaGoel
Copy link
Author

Above solution was already posted to include the jsoncpp library in cmake projects. But this solution requires jsoncppConfig.cmake file which is available if I install default jsoncpp package provided by ubuntu.

I am compiling the jsoncpp code from source. How could I link it into my cmake project if I am compiling it from source. I also don't want to use pkgconfig to search for jsoncpp

@oleurodecision
Copy link
Contributor

the jsoncppConfig.cmake is built and installed when the CMake option JSONCPP_WITH_CMAKE_PACKAGE is enabled, which is currently the default.

So configure build with cmake (some cmake -DCMAKE_BUILD_TYPE=Release -DJSONCPP_WITH_CMAKE_PACKAGE=ON -DCMAKE_INSTALL_PREFIX=[your install path] -S [path to the project source dir] -B [path to the build dir]), build and install (cmake --build [path to the build dir] --target install)

and for your consuming project, call CMake with CMAKE_PREFIX_PATH set to the root of the folder where you installed jsoncpp, and link your library/executable against jsoncpp_lib (target_link_libraries (MyGreatProject PUBLIC jsoncpp_lib)).

This should do the trick, if I don't misunderstand your question.

@baylesj
Copy link
Contributor

baylesj commented Jun 28, 2019

Fixed by #934 ... thanks @oleurodecision!!!

@clouds56
Copy link

Is it possible to make meson output the jsoncppConfig.cmake as well?
The arch linux package chose to use meson to build the jsoncpp, while I'd like to link it with another library which uses CMake

@dota17
Copy link
Member

dota17 commented Oct 14, 2020

Is it possible to make meson output the jsoncppConfig.cmake as well?

As far as I know, it can't.

The arch linux package chose to use meson to build the jsoncpp, while I'd like to link it with another library which uses CMake

You can still use cmake to build the jsoncpp, and then link it in another library.

@christian-rauch
Copy link

@clouds56 Do you need the .cmake directly? Alternatively, you can let meson generate a pkg-config file that can also be used with CMake.

@ghost
Copy link

ghost commented Jan 21, 2021

A common convention for CMake target name is using an alias target. I expected to jsoncpp target name for linking was jsoncpp::jsoncpp or jsoncpp::jsoncpp_lib instead of jsoncpp_iib.

@cdunn2001
Copy link
Contributor

Please file a PR. We use (and strongly recommend) meson instead of cmake. We rely on users for most cmake updates.

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

7 participants