1
votes

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 toxmlParseFile'

/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.

2
As I have written, I read the post, but found no solution for my problem.rpanske
Hi @rpanske! Did you find a solution to this problem now? I am compiling C++ code with SQLite and Python that used to work fine in Mac in Windows using Netbeans IDE and Cygwin compiler. I am getting same errorJMD

2 Answers

2
votes

How did you install the library ?

I came across the same problem a few days ago with Curl, and after hours and hours of Googling and digging every corner of the web to find a solution (because I'm still a newbit), I finally found it.

The solution that worked for me might not work for you, but it still worth it to give it a try, following these steps:

  • Try to build your library into a static one instead of including it (Google will help you with this but it's as easy as ./configure and make)
  • Make sure the program can use the library without including it (-v flag can help)
  • Add a compiler option -llibname (for example -llibxml). This option Search the library named libname when linking.
  • Try to compile and run the program as usual.

Goodluck !

1
votes

Looks like you are using Netbeans IDE for your C++ project. I faced similar issue and I solved it by

Right clicking on Project ==> Properties ==> Linker ==> Compilation Line ==> Additional Options ==> -llibxml 

When I was using Mac OS, I had -llibxml under

C++ compiler ==> Compilation Line ==> Additional Options

In Mac OS that worked but NOT in Windows. As Daniel D. suggested, since --llibxml must be at end of execution command. placing it in Linker helped me