我想在windows上使用cJSON库和clion。我已经将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发布于 2019-12-27 15:11:08
我刚刚解决了这个问题。我唯一需要做的就是将cJSON.h和cJSON.c添加到CMakeLists.txt的add_executable函数中,如下所示:
add_executable(ProjectName main.c cJSON.h cJSON.c)发布于 2019-12-19 22:58:25
似乎您没有将cjson.c文件添加到您的构建中,这会导致出现链接错误,或者您可能会将c文件(cjson.c)添加到c++项目中,而您的cjson.h标头中缺少'extern C‘前缀。
发布于 2019-12-19 23:10:09
检查您的CMakeLists.txt文件,在set(SOURCE_FILES)下,如果cJson文件不存在,您应该将其与您自己的文件一起添加。类似于:
set(SOURCE_FILES src/main.cpp include/main.h src/cJson.c include/cJson.h)如果这样做不起作用,或者它们已经存在,如果您正在尝试将C库与C++一起使用,则可以尝试在括号中包含完整的cJson.h内容来编写extern C {...}
https://stackoverflow.com/questions/59411762
复制相似问题