using msvc lib in mingw
i have a hardware (xray sensor) which has development tools. but afaiu these are built in msvc. so i have a .lib file and .dll file . if i include this lib file to my project (i am using qt), and put dll file to the exe folder and compile using MSVC-Release option everytihng works. But when i try to compile using mingw-Release option. it fails.
undefined reference to `imp__ZN6IDcDrv6CreateEPKci'
undefined reference to `imp__ZN6IDcDrv14GetDeviceCountEv'
can you point some way out so that i can use these lib. and dll files using mingw compiler
ps: i tried and failed impdef dclibsn.dll>dclib.def
dlltool -dllname dclibsn.dll --def dclib.def --output-lib libdclibsn.a
and this is how my def file looks like
LIBRARY "dclibsn.dll"
EXPORTS
??0DcDrv@@QAE@ABV0@@Z
??0DcDrv@@QAE@V?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@@Z
??0IDcDrv@@QAE@ABV0@@Z
??0IDcDrv@@QAE@XZ
??1DcDrv@@QAE@XZ
??4DcDrv@@QAEAAV0@ABV0@@Z
??4IDcDrv@@QAEAAV0@ABV0@@Z
?CaptureImage@DcDrv@@AAE_NHHHK_NHH@Z
?CloseUsbDevice@DcDrv@@AAE_NXZ
?ColumnDefectComp@DcDrv@@AAEGPBGH@Z
?CompensationImage@DcDrv@@AAEXPAGQAEHH@Z
?Create@IDcDrv@@SAPAVDcDrv@@PBDH@Z
note the last entry in this file (Create. ) i am trying to call this in the program and i get
(.text+0x1ad): undefined reference to `_imp___ZN6IDcDrv6CreateEPKci'
and if i replace the line ?Create@IDcDrv@@SAPAVDcDrv@@PBDH@Z
with imp__ZN6IDcDrv6CreateEPKci it compiles, but gives error.
The procedure entry point _ZN6IDcDrv6CreateEPKci could not be located in the dynamic link library dclibsn.DLL.
See this : How to use libraries compiled with MingW in MSVC?
I mean surrounding #include with extern "C" block. Because using extern "C" will instruct the compiler that the functions are using C linkage, not C++, which will stop it from performing name mangling on the functions.
I think name mangling causes problem in linkage. Don't know this will help automatic using of dll or not. Hope it will. Becuase i need to write a CUDA based dll in MSVC 2005 and use it in mingw.
Hope this help. :-?
我当然不是这方面的专家,但我一直认为使用mingw构建的项目无法使用msvc项目中的任何dll或libs,因为它们是使用不同的编译器构建的。
You have a simple problem here: The name mangling scheme, ie the mapping from C++ functions with possibly overloaded names to strings that identify functions by name only, for the MS compiler and for GCC is different. So, the function names are different, and the MingW searches for its own mangled name format ( _ZN6IDcDrv6CreateEPKci
) and can't find the microsoftly mangled name ( ?Create@IDcDrv@@SAPAVDcDrv@@PBDH@Z
).
Now, if these two libraries are truly using C++ features, this is all the better. Suppose you'd pass a C++ string from the library into your program and back - there is absolutely no guarantee that the MS implementation of the string is binary-compatible to the GNU implementation, so you'd probably end up with nasty errors.
However, if the interface of these functions is C-compatible (can be expressed in POD terms), you can declare the functions within an extern "C"
block and thus disable name mangling. It seems the developers of the lib you are building against didn't do this. If this was unnecessary (sorry, I can't demangle MSVC names to a function signature, so really can't tell), you should file a bug against the library and have them declare the interface or as large parts as possible with extern "C"
. If that is impossible, write a wrapper in MSVC that calls their functions and declares its own functions within an extern "C"
.
下一篇: 在mingw中使用msvc lib