1. 程式人生 > >有關使用cJSON包解析或者建立JSON時遇到的錯誤和解決辦法

有關使用cJSON包解析或者建立JSON時遇到的錯誤和解決辦法

一、undefined reference

/tmp/cc7DP8ZD.o: In function `parse_number':
cJSON.c:(.text+0x48f): undefined reference to `pow'
/tmp/cc7DP8ZD.o: In function `print_number':
cJSON.c:(.text+0x5b4): undefined reference to `floor'

截圖如下:
這裡寫圖片描述

解決辦法:在編譯語句後加 -lm,原因是在Unix,一些數學計算並不屬於Unix的標準庫,就像執行緒一樣,需要依賴額外的庫

 gcc -o testClient testClient.c cJSON.c poolsocket.c socketlog.c socketutil.c -I ../inc/ -lpthread -lm

截圖:
這裡寫圖片描述

二、SZ and VOID

expected ‘)’ before ‘sz’
expected ‘;’ before ‘void’

這裡寫圖片描述

解決辦法:將cJSON.h和cJSON.c分別放在標頭檔案和原始檔下即可

gcc -o testClient testClient.c cJSON.c poolsocket.c socketlog.c socketutil.c -I ../inc/ -lpthread -lm

-I ../inc/下包含所有的依賴檔案