DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.68k stars 3.21k forks source link

Can't link from CMake #808

Open thcrt opened 9 months ago

thcrt commented 9 months ago

I'm a huge beginner to C and CMake, so apologies if I've missed anything obvious.

I try to link cJSON like this:

# CMakeLists.txt

cmake_minimum_required(VERSION 3.28)
project(myproj VERSION 0.0.1
                 LANGUAGES C
)

find_package( CURL REQUIRED )
find_package( cJSON REQUIRED )
add_executable(myproj myproj.c)
target_link_libraries( myproj CURL::libcurl )
target_link_libraries( myproj cJSON::cjson )

It won't build, despite having it installed:

theo@dev ~/myproj (main)> cmake -B build
-- Configuring done (0.1s)
CMake Error at CMakeLists.txt:11 (target_link_libraries):
  Target "myproj" links to:

    cJSON::cjson

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.

theo@dev ~/myproj (main) [1]> ls /usr/lib/cmake/cJSON
cJSONConfig.cmake  cJSONConfigVersion.cmake  cjson-noconfig.cmake  cjson.cmake  cjson_utils-noconfig.cmake  cjson_utils.cmake
zeerd commented 9 months ago

The Config.cmake of cJSON has no namespace. Which means you need to use cjson but not cJSON::cjson.