Search code examples
cgccmxml

Cannot Work with mxmlGetText and mxmlGetElemet in mxml


#include <mxml.h>
#include <stdio.h>

void main()
{
    FILE *fp;

    mxml_node_t *tree;
    mxml_node_t *node  = NULL;

    fp = fopen("1.xml", "r");

    tree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK);

    node = tree;

    fprintf(stderr, "Element::%s\n", mxmlGetElement(node));
    fprintf(stderr, "Value::%s\n", mxmlGetText(node, 0));
}

Above is My Code snippet ... Error is

xmlparsing.c: In function ‘main’:
xmlparsing.c:20:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
xmlparsing.c:21:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
/tmp/ccAYsMOB.o: In function `main':
xmlparsing.c:(.text+0x58): undefined reference to `mxmlGetElement'
xmlparsing.c:(.text+0x8c): undefined reference to `mxmlGetText'
collect2: ld returned 1 exit status

Already included the mxml.h than why undefined reference? I searched about this functions on the Internet, and links show me that it's in mxml.h header file.


Solution

  • Your problem is with your Linker, see that last message:

    collect2: ld returned 1 exit status
    

    You need to inform the gcc (which is a call to a compiler and linker about the include path and the library path. The include path is where mxml.h is found, this will be done with:

    -I<path/to/include/mxml.h>
    

    The library path is given with:

    -L<path/to/shared/libmxml.a> 
    

    So in total you should have:

    gcc yourfile.c -I <path/to/include/mxml.h> -L <path/to/shared/libmxml.a>
    

    You can include other paths as needed.