I'm trying to use libxml2 on a c++ project, but i cant seem to manage to link the libraries correctly.
Here is parser.cpp (parse() comes from an example on xmlsoft.org)
#include <iostream>
#include <stdio.h>
extern "C"
{
#include <libxml/xmlreader.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
}
using namespace std;
void processNode(xmlTextReaderPtr reader){
if(reader!=NULL){
cout << "not null\n"<<endl;
}
}
void parse(const char *xmlFile){
xmlTextReaderPtr reader;
int ret;
reader = xmlReaderForFile(xmlFile, NULL, 0);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
processNode(reader);
ret = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
if (ret != 0) {
fprintf(stderr, "%s : failed to parse\n", xmlFile);
}
} else {
fprintf(stderr, "Unable to open %s\n", xmlFile);
}
}
int main(int argc, char** argv)
{
if(argc < 2){
cout << "Please provide an XML file as Argument \n"<<endl;
}
parse(argv[1]);
return 0;
}
I compile it with
g++ -I/usr/local/include/libxml2 parser.cpp
and i get the following error:
/usr/bin/ld: /tmp/ccaeOdco.o: in function `parse(char const*)':
parser.cpp:(.text+0x64): undefined reference to `xmlReaderForFile'
/usr/bin/ld: parser.cpp:(.text+0x7b): undefined reference to `xmlTextReaderRead'
/usr/bin/ld: parser.cpp:(.text+0x9c): undefined reference to `xmlTextReaderRead'
/usr/bin/ld: parser.cpp:(.text+0xad): undefined reference to `xmlFreeTextReader'
collect2: error: ld returned 1 exit status
I have verified that functions exist on the included files. What am I doing wrong?