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

半腔热情 提交于 2019-12-21 05:29:20

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/4265639/is-it-possible-to-use-a-gcc-compiled-library-with-msvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!