You are not logged in.

#1 2007-11-29 16:50:19

tntcoda
Member
Registered: 2007-07-24
Posts: 115

libxml problems

Hi,

I am trying to use the libxml2 library, and cant manage to compile some example code from the documentation sad not sure what im doing wrong, but i haven't used gcc much before.

The code:

#include <stdio.h>
#include <libxml/xmlreader.h>

#ifdef LIBXML_READER_ENABLED

/**
 * processNode:
 * @reader: the xmlReader
 *
 * Dump information about the current node
 */
static void
processNode(xmlTextReaderPtr reader) {
    const xmlChar *name, *value;

    name = xmlTextReaderConstName(reader);
    if (name == NULL)
    name = BAD_CAST "--";

    value = xmlTextReaderConstValue(reader);

    printf("%d %d %s %d %d", 
        xmlTextReaderDepth(reader),
        xmlTextReaderNodeType(reader),
        name,
        xmlTextReaderIsEmptyElement(reader),
        xmlTextReaderHasValue(reader));
    if (value == NULL)
    printf("\n");
    else {
        if (xmlStrlen(value) > 40)
            printf(" %.40s...\n", value);
        else
        printf(" %s\n", value);
    }
}

/**
 * streamFile:
 * @filename: the file name to parse
 *
 * Parse and print information about an XML file.
 */
static void
streamFile(const char *filename) {
    xmlTextReaderPtr reader;
    int ret;

    reader = xmlReaderForFile(filename, 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", filename);
        }
    } else {
        fprintf(stderr, "Unable to open %s\n", filename);
    }
}

int main(int argc, char **argv) {
    if (argc != 2)
        return(1);

    /*
     * this initialize the library and check potential ABI mismatches
     * between the version it was compiled for and the actual shared
     * library used.
     */
    LIBXML_TEST_VERSION

    streamFile(argv[1]);

    /*
     * Cleanup function for the XML library.
     */
    xmlCleanupParser();
    /*
     * this is to debug memory for regression tests
     */
    xmlMemoryDump();
    return(0);
}

#else
int main(void) {
    fprintf(stderr, "XInclude support not compiled in\n");
    exit(1);
}
#endif

Im compiling as:
g++ -isystem /usr/include/libxml2 main.cpp

The libxml2 headers are stored here: /usr/include/libxml2/libxml/

And i get lots of undefined reference errors:

/tmp/ccuri8t3.o: In function `processNode(_xmlTextReader*)':
main.cpp:(.text+0x10): undefined reference to `xmlTextReaderConstName'
main.cpp:(.text+0x2b): undefined reference to `xmlTextReaderConstValue'
main.cpp:(.text+0x39): undefined reference to `xmlTextReaderHasValue'
main.cpp:(.text+0x46): undefined reference to `xmlTextReaderIsEmptyElement'
main.cpp:(.text+0x53): undefined reference to `xmlTextReaderNodeType'
main.cpp:(.text+0x60): undefined reference to `xmlTextReaderDepth'
main.cpp:(.text+0xa4): undefined reference to `xmlStrlen'
/tmp/ccuri8t3.o: In function `streamFile(char const*)':
main.cpp:(.text+0xff): undefined reference to `xmlReaderForFile'
main.cpp:(.text+0x113): undefined reference to `xmlTextReaderRead'
main.cpp:(.text+0x12e): undefined reference to `xmlTextReaderRead'
main.cpp:(.text+0x142): undefined reference to `xmlFreeTextReader'
/tmp/ccuri8t3.o: In function `main':
main.cpp:(.text+0x1b7): undefined reference to `xmlCheckVersion'
main.cpp:(.text+0x1cf): undefined reference to `xmlCleanupParser'
main.cpp:(.text+0x1d4): undefined reference to `xmlMemoryDump'
collect2: ld returned 1 exit status

Any ideas on what i am doing wrong? I suspect i havent told gcc how to use the headers correctly? sad

Thanks

Jack

Offline

#2 2007-11-29 22:28:09

kumico
Member
Registered: 2007-09-28
Posts: 224
Website

Re: libxml problems

you also need '''-I/usr/include/libxml2
-L/usr/lib -lxml2 -lz -lm'''

compile with g++ `xml2-config --cflags --libs` main.cpp == a.out

Offline

#3 2007-11-30 00:49:12

tntcoda
Member
Registered: 2007-07-24
Posts: 115

Re: libxml problems

thanks, that worked smile

Offline

Board footer

Powered by FluxBB