C語言解析xml檔案遇到的乙個編譯問題

2021-08-26 21:09:44 字數 2652 閱讀 8652

在用libxml2嘗試做解析xml檔案,編譯的時候出現了找不到標頭檔案的錯誤:

[nigelzeng@myhost xml-learning]$ gcc -o xml-build xml-build.c               

xml-build.c:4:30: libxml/xmlmemory.h: 沒有那個檔案或目錄

xml-build.c:5:27: libxml/parser.h: 沒有那個檔案或目錄

xml-build.c:8: error: syntax error before "doc"

xml-build.c: in function `parsestory':

xml-build.c:9: error: `xmlchar' undeclared (first use in this function)

xml-build.c:9: error: (each undeclared identifier is reported only once

……

問題出自啊c檔案裡的include:

#include #include
缺省會到/usr/include 目錄下搜尋,但是不存在libxml目錄,而libxml是在/usr/include/libxml2/libxml 下。

所以我先做了乙個軟連線:

[nigelzeng@myhost xml-learning]$ ln -s /usr/include/libxml2/libxml /usr/include/libxml
[nigelzeng@myhost xml-learning]$ gcc -o xml-build xml-build.c

/tmp/cc62wqck.o(.text+0x24): in function `parsestory':

: undefined reference to `xmlstrcmp'

/tmp/cc62wqck.o(.text+0x3e): in function `parsestory':

: undefined reference to `xmlnodelistgetstring'

/tmp/cc62wqck.o(.text+0x62): in function `parsestory':

: undefined reference to `xmlfree'

/tmp/cc62wqck.o(.text+0x85): in function `parsedoc':

: undefined reference to `xmlparsefile'

/tmp/cc62wqck.o(.text+0xb7): in function `parsedoc':

: undefined reference to `xmldocgetrootelement'

/tmp/cc62wqck.o(.text+0xe4): in function `parsedoc':

: undefined reference to `xmlfreedoc'

/tmp/cc62wqck.o(.text+0xff): in function `parsedoc':

: undefined reference to `xmlstrcmp'

/tmp/cc62wqck.o(.text+0x127): in function `parsedoc':

: undefined reference to `xmlfreedoc'

/tmp/cc62wqck.o(.text+0x14e): in function `parsedoc':

: undefined reference to `xmlstrcmp'

/tmp/cc62wqck.o(.text+0x17c): in function `parsedoc':

: undefined reference to `xmlfreedoc'

collect2: ld returned 1 exit status

在網上搜尋了下答案,可以在gcc命令後加上-i-l引數解決:

[nigelzeng@myhost xml-learning]$ gcc -o xml-build xml-build.c -i/usr/include/libxml2/ -lxml2
用這個命令,所有的錯誤都解決了,成功編譯出來需要的執行檔案。

這個-i-l的引數解釋是這樣的:

-idir 當用#include 鏈結檔案的時候,gcc/g++會先在當前目錄查詢你所制定的標頭檔案,如果沒有找到,他回到預設的頭檔案目錄找,如果使用-i制定了目錄,他會先在你所制定的目錄查詢,然後再按常規的順序去找。

-llibrary 制定編譯的時候使用的庫。

例子用法: gcc -lcurses hello.c   /* 使用ncurses庫編譯程式 */

所以「gcc -o xml-build xml-build.c -i/usr/include/libxml2/ -lxml2」命令的意思就是「在指定目錄/usr/include/libxml2裡尋找標頭檔案,編譯時使用的庫是xml2函式庫」

ps:這個命令也可以成功編譯:gcc `xml2-config --cflags --libs` -o xml-create xml-create.c

乙個C 的 XML 解析程式

乙個c 的 xml 解析程式 下面是乙個示例 xml 檔案 在 程式 include to get printf function include to get free function include xmlparse r.h intmain intargc,char argv 載入xml xm...

遇到乙個C語言的神奇問題

比較兩個數字絕對值的大小,函式abs include include void main else if x1 y1 0 getchar 判定輸入的字元是數字,大寫字母,小寫字母或其他字元。include void main else if i 65 i 90 else if i 97 i 122 ...

乙個使用tingxml解析XML配置檔案的例子

配置檔案如下所示 user 20141111 wwwhost ffffdbname sauser 123456password db d downloadpath 3timeout config 讀取配置檔案的函式實現如下所示 bool readconfigfile cstringa str str...