作者:O八戒有点坏O | 来源:互联网 | 2023-09-09 15:02
我想在带有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
似乎您没有将cjson.c
文件添加到构建中,这会导致链接错误,或者您可能将c文件(cjson.c
)添加到c ++项目中,而您丢失了' cjson.h
标头中的extern C'前缀。
,
检查您的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
>
,
我刚刚解决了这个问题。我唯一需要做的就是将cJSON.h和cJSON.c添加到CMakeLists.txt的add_executable函数中,如下所示:
add_executable(ProjectName main.c cJSON.h cJSON.c)