json-c / json-c

https://github.com/json-c/json-c is the official code repository for json-c. See the wiki for release tarballs for download. API docs at http://json-c.github.io/json-c/
https://github.com/json-c/json-c/wiki
Other
2.9k stars 1.05k forks source link

Cannot link properly using cmake #838

Closed fluca1978 closed 8 months ago

fluca1978 commented 8 months ago

I'm trying to use json-c in a project that uses cmake, the project is https://github.com/agroal/pgagroal and I've installed json-c as follows:

$ sudo dnf install json-c

$ lsb_release -a
LSB Version:    :core-4.1-ia32:core-4.1-noarch
Distributor ID: Rocky
Description:    Rocky Linux release 8.7 (Green Obsidian)
Release:        8.7
Codename:       GreenObsidian

Then I've added into CMakeList.txt:

find_package(json-c CONFIG)
target_link_libraries(${PROJECT_NAME} PRIVATE json-c::json-c)

however when I try to build, I got:

$ cmake .. -DCMAKE_PREFIX_PATH=/usr/local/lib64/cmake/json-c  && make clean && cmake .. && make && sudo make install
-- pgagroal 1.6.0
-- Build type is Release
-- System is Linux
-- libev found
-- OpenSSL found
-- rst2man found
-- libatomic found
-- systemd found
CMake Error at CMakeLists.txt:98 (target_link_libraries):
  Cannot specify link libraries for target "pgagroal" which is not built by
  this project.

-- Configuring incomplete, errors occurred!
See also "/home/luca/pgagroal/build/CMakeFiles/CMakeOutput.log".

The cmake log has nothing related to json-c. I then noted that:

$ pkg-config --libs json-c
Package json-c was not found in the pkg-config search path.
Perhaps you should add the directory containing `json-c.pc'
to the PKG_CONFIG_PATH environment variable
Package 'json-c', required by 'virtual:world', not found
``

so I tried to add the directory but the build still fails:

$ echo $PKG_CONFIG_PATH /usr/local/lib64/pkgconfig/ $ export PKG_CONFIG_PATH $ pkg-config --libs json-c -L/usr/local/lib64 -ljson-c

$ cmake .. -DCMAKE_PREFIX_PATH=/usr/local/lib64/cmake/json-c && make clean && cmake .. && make && sudo make install -- pgagroal 1.6.0 -- Build type is Release -- System is Linux -- libev found -- OpenSSL found -- rst2man found -- libatomic found -- systemd found CMake Error at CMakeLists.txt:98 (target_link_libraries): Cannot specify link libraries for target "pgagroal" which is not built by this project.

-- Configuring incomplete, errors occurred!



I have no idea about what am I missing here. Any suggestion is welcome.
hawicz commented 8 months ago

This doesn't sound like a json-c problem. Do you have an add_library or add_executable line in your project's CMakeLists.txt?

fluca1978 commented 8 months ago

Good guess @hawicz ! It turned out that CMakeList.txt was calling a subdirectory CMakeList.txt, and is the latter one that requires the

find_package(json-c CONFIG)
target_link_libraries(${PROJECT_NAME} PRIVATE json-c::json-c)

addition. Now I'm an happy json-c user!