mingw/libxml2 issue

2019-07-16 12:07发布

问题:

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...

回答1:

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.



回答2:

This can be fixed by using the "--disable-shared" flag to configure, e.g.:

./configure --prefix=/mingw --disable-shared



回答3:

Try adding -DIN_LIBXML to your compile flags (from related issue).



回答4:

You've probably long since fixed this but just in case, try it with -lxml2. That is drop the lib prefix from the library name. You might also need to use the -L option to set the search path. Specify -L before -l on the command line.



标签: mingw libxml2