-->

gcc newly installed libraries (libexpat1-dev) not

2019-05-24 07:40发布

问题:

I've spent quite a bit of time trying to get an expat based sample program to compile.

I was receiving the following error message when I tried to compile

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

I had already run the following commands: sudo apt-get install expat libexpat1 libexpat1-dev libxmltok1-dev

So I messed around for ages, trying to get the error message to go away so I could compile this (simple) little program, but to no avail.

Eventually, out of sheer randomness, I decided to switch to a Virtual Terminal.

I ran exactly the same command, this time it worked without a problem.

Can anyone tell me why this is? Is there something I need to run in order to refresh the library paths?

回答1:

The linker line (i.e. the flags you give with -l and your input file names) are order-dependent. Libraries are only used to define functions used on their left side, but not on their right. Use:

 gcc -Wall line.c  -lexpat -o blah

See the question library is linked but reference is undefined for more information.