user140053
user140053

Reputation:

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

Upvotes: 4

Views: 4099

Answers (5)

Andrea Moreschi
Andrea Moreschi

Reputation: 21

for me the solution presented by lano1106 worked well. I recompiled the libxml2-2.9.9 with MinGW (following these steps:

https://blog.inventic.eu/2010/11/how-to-compile-open-source-libraries-under-windows-using-mingw/)

I encountered a compilation error with nanohttp.c present only in MinGW 32 (not prensent in MinGW64 they say), but i fixed it editing the file as shown in this thread:

https://gitlab.gnome.org/GNOME/libxml2/commit/d3de75782504c9136e504c6356bbae52fedf17e5

Then in Eclipse IDE, in Windows 10:

In Project -> Properties -> C/C++ General -> Paths and Symbols -> Includes Tab -> GNU C++ -> Added my_path\libxml2\include

In Project -> Properties -> C/C++ Build -> Settings -> Tool Settings Tab -> MinGW C++ Linker -> Libraries

In Libraries (-l) -> Added xml2 In Library Search Path (-L) -> Added C:\libxml2.libs

Then save and recompile.

Thank you for the solution and i hope my answer could be useful or clarifying for someone!

Upvotes: 2

Niklas Holm
Niklas Holm

Reputation: 1028

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

Upvotes: 2

SubJunk
SubJunk

Reputation: 312

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

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

Upvotes: 4

lano1106
lano1106

Reputation: 168

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.

Upvotes: 2

Ishmael
Ishmael

Reputation: 478

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.

Upvotes: 1

Related Questions