MSVC可以使用gcc编译库吗? [英] Is it possible to use a gcc compiled library with MSVC?
问题描述
我有一个项目依赖 libiconv
进行多项操作。
我使用预编译的二进制文件来处理 iconv.lib
用于Visual Studio 2008,但现在我必须转移到Visual Studio 2010,并且不再提供预编译的二进制文件。
我决定自己编译它,但是作为 libiconv
文档指出,没有官方支持MSVC编译器。然而,我读过 gcc
可以产生与MSVC编译器二进制兼容的静态库,只要二进制接口保持在 C
。虽然听起来很疯狂,但我试了一下,实际上它几乎可以工作。
我编译了它,重命名为 libiconv.a
转换为 iconv.lib
,并试图链接它。 (如果这是一个坏主意,请告诉我)。
首先我遇到了一个链接错误:
1> iconv.lib(iconv.o):错误LNK2001:无法解析的外部符号___chkstk
经过一番研究,我重新编译了 libiconv
(x86和x64版本),添加了 -static-libgcc
flag。
它可以工作,但仅适用于我的程序的x64版本。我们应该怎么做才能做到这一点?
是的,这是可能的,因为如果您严格按照C接口进行编译,iconv将链接到 msvcrt.lib
,这是Windows C运行时(严格来说,类似于GCC使用的 glibc
)。如果使用GCC的普通C ++编译器构建它,它将链接到 libstdc ++
,并且命名为mangle的方式与链接到 msvcr.dll
。
在您的情况下, __ chkstk
是检查堆栈函数(C运行时的一部分),这是一个注入每个函数调用以检查堆栈溢出的函数。我不知道如何解决这个'好'的方法,但你可以改变你的构建选项,让iconv使用额外的编译器标记来构建,并取消这个检查: / Gs999999
但是,我已经用Visual C ++ 2005/2008/2010构建了libiconv,并且我已经在其上构建了软件完美(软件使用的军事机构,如果你需要信誉)。我确实记得使用VC ++编译器有点烦人,但它不应该太痛苦。
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 ?
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.
这篇关于MSVC可以使用gcc编译库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!