Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a gcc compiled library with MSVC?

I have a project that relies on libiconv for several operations.

I was using precompiled binaries for iconv.lib for Visual Studio 2008 but now I had to move on to Visual Studio 2010 and no more precompiled binaries were available.

I decided to compile it myself but as the libiconv documentation states, there is no official support for MSVC compilers. However, I read somewhere that gcc could generate static libraries that were binary compatible with MSVC compilers, as long as the binary interface remains in C. While that sounded crazy, I gave it a try and it actually almost worked.

I compiled it, renamed libiconv.a to iconv.lib and tried to link with it. (If this is a bad idea, please let me know).

First I encountered a link error:

1>iconv.lib(iconv.o) : error LNK2001: unresolved external symbol ___chkstk

After some research, I recompiled libiconv (in both x86 and x64 version), adding the -static-libgcc flag.

It worked, but only for the x64 release of my program. The x86 release always fails with the very same error.

What should I do to make this work ?

like image 867
ereOn Avatar asked Nov 24 '10 10:11

ereOn


1 Answers

Yes, this is possible because if you compile strictly as a C interface, iconv will link to msvcrt.lib, which is the Windows C runtime (strictly C, analogous to glibc used by GCC). If you built it with GCC's normal C++ compiler, it would link to libstdc++ and also name mangle differently compared to VC++, which links to msvcr.dll.

In your case, __chkstk is the check stack function (part of C runtime), a function that is injected into each function call to check for stack overflows. I am not sure how to resolve this a 'good' way, but you can change your build options for iconv to build with an additional compiler flag and suppress this check: /Gs999999


However, I have built libiconv with Visual C++ 2005/2008/2010, and software I have built on top of it worked perfectly (software used by military institutions, if you need credibility). I do remember it being slightly annoying to build using the VC++ compiler though, but it should not be too much pain.

like image 70
逆さま Avatar answered Sep 23 '22 20:09

逆さま