0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Or with pkg-config, you can use $(pkg-config --libs libxml-2.0) instead of -lxml2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.