下面把我常遇到的 compile error 列出來:
No such file or directory
$ gcc -o test test.c test.c:2:23: error: json/json.h: No such file or directory test.c: In function 'main': test.c:8: warning: assignment makes pointer from integer without a cast test.c:12: warning: assignment makes pointer from integer without a cast表示 include 的 header file 找不到,預設會去找 /usr/include、/usr/local/include,若 library 安裝在其他目錄,可以在 compile 時加上 -I/opt/json-c-0.9/include。
Undefined reference
$ gcc -o test test.c -I/opt/json-c-0.9/include /tmp/ccMCrNFo.o: In function `main': test.c:(.text+0x9): undefined reference to `json_object_new_object' test.c:(.text+0x17): undefined reference to `json_object_new_string ... test.c:(.text+0xc1): undefined reference to `json_object_put' collect2: ld returned 1 exit status表示 link library 失敗,compile 時要加上 -ljson
Linking fail
$ gcc -o test test.c -I/opt/json-c-0.9/include -ljson /usr/bin/ld: cannot find -ljson collect2: ld returned 1 exit status表示找不到 libjson.so 的 library 檔案,預設的搜尋路徑 /lib、/lib64、/usr/lib、/usr/local/lib, 若 library 安裝在其他地方,在 compile 時要加上 -L/opt/json-c-0.9/lib 來增加搜尋的路徑。
Implicit declaration of function
$ gcc -Wall -o test test.c test2.o -I/opt/json-c-0.9/include -L/opt/json-c-0.9/lib -ljson test.c: In function 'main': test.c:23: warning: implicit declaration of function 'sub_func'出現此 warning,表示你用的 function 在 .c 檔中有定義,但是没有在對應的 header 檔中宣告。
上例中我在 test.c 去 call sub_func() 這個 function,這個 function 是在 test2.c 中 implement,但卻沒有在 test2.h 宣告。
Error while loading shared libraries
$./test ./test: error while loading shared libraries: libjson.so.0: cannot open shared object file: No such file or directory表示 runtime 時 load 不到指定的 library,環境變數 $LD_LIBRARY_PATH 要加入 share library 路徑,LD_LIBRARY_PATH=/opt/json-c-0.9/lib