【问题标题】:Using cJSON in windows在 Windows 中使用 cJSON
【发布时间】:2019-12-27 15:11:07
【问题描述】:

我想在带有 clion 的 Windows 上使用 cJSON 库。我已将 cJSON.h 和 cJSON.c 复制到我的项目目录中。我在这个命令中包含了 cJSON.h:#include "cJSON.h" 当我尝试编译我的项目时,出现以下错误:

CMakeFiles\Client.dir/objects.a(main.c.obj): In function `parse':
.../main.c:117: undefined reference to `cJSON_Parse@4'
.../main.c:121: undefined reference to `cJSON_GetErrorPtr@0'
.../main.c:127: undefined reference to `cJSON_GetObjectItemCaseSensitive@8'
.../main.c:128: undefined reference to `cJSON_GetObjectItemCaseSensitive@8'
.../main.c:131: undefined reference to `cJSON_IsString@4'
.../main.c:131: undefined reference to `cJSON_IsString@4'
.../main.c:141: undefined reference to `cJSON_Delete@4'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\Client.dir\build.make:84: recipe for target 'Client.exe' failed
mingw32-make.exe[3]: *** [Client.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Client.dir/rule] Error 2
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/Client.dir/all' failed
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/Client.dir/rule' failed
Makefile:117: recipe for target 'Client' failed
mingw32-make.exe: *** [Client] Error 2

【问题讨论】:

    标签: c json cjson


    【解决方案1】:

    似乎您没有将cjson.c 文件添加到您的构建中,这会导致您得到链接错误,或者您可能会将c 文件(cjson.c)添加到c++ 项目中,并且您缺少“extern C”前缀在您的 cjson.h 标题处。

    【讨论】:

      【解决方案2】:

      检查您的CMakeLists.txt 文件,在set(SOURCE_FILES) 下您应该添加cJson 文件(如果尚未与您自己的文件一起存在)。比如:

      set(SOURCE_FILES src/main.cpp include/main.h src/cJson.c include/cJson.h)
      

      如果这不起作用或者如果它们已经存在,如果您尝试将 C 库与 C++ 一起使用,您可以尝试在括号之间编写 extern C {...} 整个 cJson.h 内容

      【讨论】:

      • 由于我使用的是 c 并且所有文件都在同一个目录中,所以我更改了您的 set 命令,我得到了同样的错误:set(SOURCE_FILES main.c cJSON.c cJSON.h)
      【解决方案3】:

      我刚刚解决了这个问题。我唯一需要做的就是将 cJSON.h 和 cJSON.c 添加到 CMakeLists.txt 的 add_executable 函数中,如下所示:

      add_executable(ProjectName main.c cJSON.h cJSON.c)
      

      【讨论】:

        猜你喜欢
        • 2019-09-30
        • 1970-01-01
        • 1970-01-01
        • 2014-06-15
        • 1970-01-01
        • 2021-05-21
        • 2017-09-19
        • 2013-06-25
        • 2014-11-27
        相关资源
        最近更新 更多