是否可以将 gcc 编译的库与 MSVC 一起使用?
我有一个依赖 libiconv
进行多项操作的项目。
我在 Visual Studio 2008 中使用 iconv.lib
的预编译二进制文件,但现在我必须转向 Visual Studio 2010,并且不再有可用的预编译二进制文件。
我决定自己编译它,但正如 libiconv 文档所述,MSVC 编译器没有官方支持。但是,我在某处读到,只要二进制接口保留在 C 中,gcc
就可以生成与 MSVC 编译器二进制兼容的静态库。虽然这听起来很疯狂,但我尝试了一下,实际上几乎成功了。
我编译了它,将 libiconv.a 重命名为 iconv.lib 并尝试与其链接。 (如果这是一个坏主意,请告诉我)。
首先,我遇到了一个链接错误:
1>iconv.lib(iconv.o) : error LNK2001: unresolved external symbol ___chkstk
经过一番研究,我重新编译了 libiconv
(x86 和 x64 版本),添加了 -static-libgcc
标志。
它有效,但仅适用于我的程序的 x64 版本。 x86 版本总是失败并出现完全相同的错误。
我应该做什么才能使这项工作成功?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的,因为如果您严格编译为 C 接口, iconv 将链接到
msvcrt.lib
,这是 Windows C 运行时(严格来说是 C,类似于glibc
海湾合作委员会使用)。如果您使用 GCC 的普通 C++ 编译器构建它,它将链接到libstdc++
,并且与链接到msvcr.dll
的 VC++ 相比,mangle 的名称也不同。在您的情况下,
__chkstk
是检查堆栈函数(C 的一部分运行时),注入到每个函数调用中以检查堆栈溢出的函数。我不确定如何以“好”方式解决此问题,但您可以更改 iconv 的构建选项以使用附加编译器标志进行构建并禁止此检查:/Gs999999
然而,我已经用 Visual C++ 2005/2008/2010 构建了 libiconv,并且我在其之上构建的软件运行得很好(军事机构使用的软件,如果你需要可信度的话)。我确实记得使用 VC++ 编译器构建有点烦人,但应该不会太痛苦。
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 toglibc
used by GCC). If you built it with GCC's normal C++ compiler, it would link tolibstdc++
and also name mangle differently compared to VC++, which links tomsvcr.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.