我花了很多时间试图让一个基于expat的示例程序进行编译。
我在尝试编译时收到以下错误消息
gcc -Wall -lexpat line.c -o blah
line.c: In function ‘main’:
line.c:99:8: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘XML_Size’ [-Wformat]
/tmp/ccUa3vfD.o: In function `printcurrent':
line.c:(.text+0x42): undefined reference to `XML_SetDefaultHandler'
line.c:(.text+0x4d): undefined reference to `XML_DefaultCurrent'
line.c:(.text+0x60): undefined reference to `XML_SetDefaultHandler'
/tmp/ccUa3vfD.o: In function `main':
line.c:(.text+0x162): undefined reference to `XML_ParserCreate'
line.c:(.text+0x1ad): undefined reference to `XML_UseParserAsHandlerArg'
line.c:(.text+0x1c9): undefined reference to `XML_SetElementHandler'
line.c:(.text+0x1dd): undefined reference to `XML_SetCharacterDataHandler'
line.c:(.text+0x1f1): undefined reference to `XML_SetProcessingInstructionHandler'
line.c:(.text+0x2b2): undefined reference to `XML_Parse'
line.c:(.text+0x2c2): undefined reference to `XML_GetErrorCode'
line.c:(.text+0x2ca): undefined reference to `XML_ErrorString'
line.c:(.text+0x2d8): undefined reference to `XML_GetCurrentLineNumber'
collect2: ld returned 1 exit status
我已经运行了以下命令: sudo apt-get install expat libexpat1 libexpat1-dev libxmltok1-dev
所以我搞砸了很多年,试图让错误消息消失,所以我可以编译这个(简单的)小程序,但无济于事。
最终,出于纯粹的随机性,我决定切换到虚拟终端。
我运行完全相同的命令,这次它没有问题。
谁能告诉我为什么会这样?我需要运行什么才能刷新库路径?
答案 0 :(得分:4)
链接器行(即您使用-l
提供的标志和输入文件名)与顺序相关。库只用于定义左侧使用的功能,但不能用于右侧。使用:
gcc -Wall line.c -lexpat -o blah
有关详细信息,请参阅问题library is linked but reference is undefined。