Search code examples
mingwlibxml2

mingw/libxml2 issue


I'm trying to use libxml2 (v2.2.6.0) with mingw under win7 I added the lib -llibmlx2, but each time I compile I get :

error: undefined reference to `_imp__xmlFree'

On Google I found this: http://osdir.com/ml/gnome.lib.xml.ge.../msg00003.html
But still doesn't work.

Any idea ?

thanks...


Solution

  • I believe that this problem only happens when using precompiled libxml2 binaries prepared by Igor Zlatovic. I suspect that the problem goes away if using libxml2 lib natively compiled with mingw.

    Here is the declaration of xmlFree in libxml/globals.h:

    #else /* !LIBXML_THREAD_ALLOC_ENABLED */
    XMLPUBVAR xmlMallocFunc xmlMalloc;
    XMLPUBVAR xmlMallocFunc xmlMallocAtomic;
    XMLPUBVAR xmlReallocFunc xmlRealloc;
    XMLPUBVAR xmlFreeFunc xmlFree;
    XMLPUBVAR xmlStrdupFunc xmlMemStrdup;
    #endif /* LIBXML_THREAD_ALLOC_ENABLED */
    

    and XMLPUBVAR is expanded by

    #define XMLPUBVAR __declspec(dllimport) extern
    

    from libxml/xmlexports.h

    First observation. xmlFree is not a function but a variable. This is an exceptional occurrence as almost all the other libxml2 functions are real exported functions. This explain why xmlFree() is the only problematic function encountered by people.

    mingw compiler change the imported symbol name from xmlFree to _imp__xmlFree which is not found in libxml2.lib. I am guessing that with other compilers such as MSVC, the linker is instead searching for the correct symbol _xmlFree found inside libxml2.lib:

    C:\MinGW\msys\1.0\home\100517891\embedded\ext\libxml2\lib>"c:\Program Files\Microsoft Visual Studio 9.0\VC\bin\dumpbin.exe" /exports libxml2.lib | grep xmlFree
                  _xmlFree
                  _xmlFreeAttributeTable
                  _xmlFreeAutomata
                  _xmlFreeCatalog
                  _xmlFreeDoc
                  _xmlFreeDocElementContent
                  _xmlFreeDtd
                  _xmlFreeElementContent
                  _xmlFreeElementTable
                  _xmlFreeEntitiesTable
                  _xmlFreeEnumeration
                  _xmlFreeIDTable
                  _xmlFreeInputStream
                  _xmlFreeMutex
                  _xmlFreeNode
                  _xmlFreeNodeList
                  _xmlFreeNotationTable
                  _xmlFreeNs
                  _xmlFreeNsList
                  _xmlFreeParserCtxt
                  _xmlFreeParserInputBuffer
                  _xmlFreePattern
                  _xmlFreePatternList
                  _xmlFreeProp
                  _xmlFreePropList
                  _xmlFreeRMutex
                  _xmlFreeRefTable
                  _xmlFreeStreamCtxt
                  _xmlFreeTextReader
                  _xmlFreeTextWriter
                  _xmlFreeURI
                  _xmlFreeValidCtxt
    

    I have not found any way to fix the linkage error but a simple and safe way to workaround the problem is to acknowledge the fact that xmlFree is a simple function pointer variable initialized to the address of the standard free() function in globals.c and the only way to change that assignation is to recompile the dll with some debug switches.

    Feel free to replace calls to xmlFree() with calls to free(). Everything should be fine and the linkage error will go away.