Juiposa
Juiposa

Reputation: 3

GCC does not recognise libxml2 functions

When trying to compile a simple parsing program using libxml2, GCC returns this error.

/tmp/ccuq3Hc1.o: In function `main':
xmltesting.c:(.text+0x31): undefined reference to `htmlCreatePushParserCtxt'
xmltesting.c:(.text+0x50): undefined reference to `htmlCtxtReadFile'
xmltesting.c:(.text+0x64): undefined reference to `xmlDocGetRootElement'
collect2: error: ld returned 1 exit status

Code:

#include <stdio.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/tree.h>
#include <libxml/parser.h>

int main()
{

    htmlDocPtr doc;
    htmlNodePtr org_node;
    htmlNodePtr curnt_node = NULL;

    htmlParserCtxtPtr parser = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL, 0);

    doc = htmlCtxtReadFile( parser, "html.txt", NULL, 0);
    org_node = xmlDocGetRootElement( parser->myDoc );

    for ( curnt_node = org_node; curnt_node; curnt_node =  curnt_node->next ) {

        if ( curnt_node->type == XML_TEXT_NODE ) {

            printf ("%s", curnt_node->content );

        }

    }

}

It seems to be reading the structs fine, but the functions are not to be found?

Or is something wrong with my code?

Upvotes: 0

Views: 475

Answers (1)

cnicutar
cnicutar

Reputation: 182664

You probably simply have to tack an -lxml2 at the end of the gcc build line.


As Dietricg Epp points out in the comments, if your library has the right .pc file pkg-config is the preferred way to get the neccesary flags. You can get both compile and link flags.

$ pkg-config libxml-2.0 --cflags --libs
-I/usr/include/libxml2  -lxml2

Upvotes: 1

Related Questions