1

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

I am starting to develop in C and want to use the libxml2 C-Library for writing a simple xml file.

For this purpose i created seperate sourcefiles which should provide functions to write a xml file.

xmlutils.h:

#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>


void writeXML(const char *uri);

xmlutils.c:

#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>

void writeXML(const char *uri){

int rc;
xmlTextWriterPtr writer;
xmlChar *tmp;

 /* Create a new XmlWriter for uri, with no compression. */
writer = xmlNewTextWriterFilename(uri, 0);
if (writer == NULL) {
    printf("testXmlwriterFilename: Error creating the xml writer\n");
    return;
}

 //more code...


}

in my main.c file i want to use this writeXML function:

#include<stdio.h>
#include<string.h>
#include "xmlutils.h"

main() {

writeXML("test.xml");
}

Im compiling with:

gcc -o myapp main.c xmlutils.c -I/usr/include/libxml2

and i get:

xmlutils.c:(.text+0x19): undefined reference to `xmlNewTextWriterFilename'
collect2: ld returned 1 exit status

What am i doing wrong here? if i take a look at the usr/include/libxml2/libxml/xmlwriter.h file there IS the function...

thx

Community
  • 1
  • 1
Moonlit
  • 5,171
  • 14
  • 57
  • 95

2 Answers2

2

Don't forget the option -lxml2 after the objects you want to link.

gcc -o myapp main.c xmlutils.c -I/usr/include/libxml2 -lxml2 
Fred
  • 16,367
  • 6
  • 50
  • 65
  • thank you now it works! I am wondering why i have to specify the path to the libxml2 library here. Why do i have to do this, since i already include the library in code via #include ? could you please explain this? :) – Moonlit Feb 02 '13 at 19:45
  • you include the definition of it, but not the compiled code itself. So basically you need to tell the linker where he can locate the code which matches the files you included. This way gcc is able to correctly compile and link all the code. – Fred Feb 02 '13 at 19:50
0

You're missing the -l option to link libxml.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711