Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use libraries compiled with MingW in MSVC?

I have compiled several libraries with MingW/MSYS... the generated static libraries are always .a files. When I try to link the library with a MSVC project, Visual Studio throws 'unresolved external symbols' ... It means that the .a static library is incompatible with MS C++ Linker. I presume it has to be converted to a MSVC compatible .lib file.

Either .a and .lib are just AR archives of .o or .obj files, so is there any way how to use MingW compiled libs in a MSVC project? Or do I have to compile/link everything just in one compiler/linker - MSVC only/MingW only? The MingW compiler is said to be compatible with MSVC.

I read a few threads about this topic, but they mostly say that renaming the file to .lib should do the work, but it unfortunately doesn't work for me.

The libraries Im trying to link are written in C.

MSVC Linker throws errors like:

error LNK2019: unresolved external symbol "int __cdecl openssl_call(struct ssl_State *,int,int,int)" (?openssl_call@@YAHPAUssl_State@@HHH@Z) referenced in function _main MyAPP.obj

... and 4 more same errors referring to other functions called from my app.

Thanks for any advice.

like image 641
NumberFour Avatar asked Mar 27 '10 15:03

NumberFour


People also ask

Should I use MinGW or MSVC?

MSVC is doing the compilation job significantly faster than MinGW-w64. The DLL sizes are comparable, if optimization is set to "-O2" for MinGW-w64, with "-O3" the DLLs from MinGW-w64 are larger. Binary files compiled with MinGW-w64 are performing significantly better than those compiled with MSVC.

What is MSVC and MinGW?

Basically, MinGW is a port of Linux (GNU, actually) tools for Windows, which happen to include a port of GCC (the actual compiler). MSVC, on the other hand, is a Windows native tool chain and runtime for building C/C++ executables.

How do I create a DLL file in MinGW?

Open a command prompt, change to the directory where you extracted the files, and type "mingw32-make". The DLL and executable should be compiled, linked, and output as "AddLib. dll" and "AddTest.exe" in the "bin" directory. The import library will be created as "libaddlib.


1 Answers

Based on this error you put in a comment:

error LNK2019: unresolved external symbol "int __cdecl openssl_call(struct ssl_State *,int,int,int)" (?openssl_call@@YAHPAUssl_State@@HHH@Z) referenced in function _main MyAPP.obj all other 4 errors are same only with other functions names

Try putting extern "C" around your include files for openssl. For example:

extern "C" {
include "openssl.h"
}

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. So it will look for the function openssl_call in the library rather than ?openssl_call@@YAHPAUssl_State@@HHH@.

like image 144
shf301 Avatar answered Oct 06 '22 00:10

shf301