I'm trying to use the libxml2 library in C++, but I get some errors which I do not know how to solve. My code is a basic example of the library.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
void
parseStory (xmlDocPtr doc, xmlNodePtr cur) {
xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("keyword: %s\n", key);
xmlFree(key);
}
cur = cur->next;
}
return;
}
static void
parseDoc(char *docname) {
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile(docname);
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
parseStory (doc, cur);
}
cur = cur->next;
}
xmlFreeDoc(doc);
return;
}
int
main(int argc, char **argv) {
char *docname;
if (argc <= 1) {
printf("Usage: %s docname\n", argv[0]);
return(0);
}
docname = argv[1];
parseDoc (docname);
return (1);
}
And I get these errors while building the project.
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:26: undefined reference to `xmlStrcmp'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:26:(.text+0x36): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlStrcmp'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:27: undefined reference to `xmlNodeListGetString'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:27:(.text+0x59): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlNodeListGetString'
build/Debug/Cygwin-Windows/main.o: In function
parseDoc': /cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:42: undefined reference to
xmlParseFile'/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:42:(.text+0xad): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlParseFile'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:49: undefined reference to `xmlDocGetRootElement'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:49:(.text+0xec): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlDocGetRootElement'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:53: undefined reference to `xmlFreeDoc'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:53:(.text+0x126): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlFreeDoc'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:57: undefined reference to `xmlStrcmp'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:57:(.text+0x142): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlStrcmp'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:59: undefined reference to `xmlFreeDoc'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:59:(.text+0x17a): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlFreeDoc'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:65: undefined reference to `xmlStrcmp'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:65:(.text+0x1a6): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlStrcmp'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:72: undefined reference to `xmlFreeDoc'
/cygdrive/c/Users/robin/Documents/NetBeansProjects/xml/main.cpp:72:(.text+0x1d9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `xmlFreeDoc'
build/Debug/Cygwin-Windows/main.o:main.cpp:(.rdata$.refptr.xmlFree[.refptr.xmlFree]+0x0): undefined reference to `xmlFree'
I've read this post What does this GCC error "... relocation truncated to fit..." mean?, but I have no clue how to solve my problem. My IDE is Netbeans 8.2 with Cygwin64 on a Windows 10 64 bit machine. I've included libxml2-2.9.3-win32-x86_64.7z and iconv-1.14-win32-x86_64.7z. I hope you can help me, thank you.