简体   繁体   English

静态链接 GLFW 时对“__ms_vsnprintf”的未定义引用

[英]Undefined reference to `__ms_vsnprintf' when linking GLFW statically

I am trying to compile the GLFW quickstart guide ( Here ) in windows 8 64-bit using mingw.我正在尝试使用 mingw 在 Windows 8 64 位中编译 GLFW 快速入门指南(此处)。 I am using the official 32 bit windows binary from the glfw website.我正在使用 glfw 网站上的官方 32 位 Windows 二进制文件。

Everything works fine when I link the glfw library dynamically by linking -lglfw3dll -lgdi32 -lopengl32 -lglew32 and defining GLFW_DLL .当我通过链接-lglfw3dll -lgdi32 -lopengl32 -lglew32并定义GLFW_DLL动态链接 glfw 库时,一切正常。

When I try to link glfw statically, however, I get undefined reference to '__ms_vsnprintf'然而,当我尝试静态链接 glfw 时,我得到了undefined reference to '__ms_vsnprintf'

My command to link statically is mingw32-g++.exe -o bin\\Release\\test.exe obj\\Release\\main.o -s -lglfw3 -lgdi32 -lopengl32 -lglew32s with GLEW_STATIC defined.我静态链接的命令是mingw32-g++.exe -o bin\\Release\\test.exe obj\\Release\\main.o -s -lglfw3 -lgdi32 -lopengl32 -lglew32s GLEW_STATIC定义了GLEW_STATIC

I got the same problem when I tried to build an example app for GLFW.当我尝试为 GLFW 构建示例应用程序时,我遇到了同样的问题。 I switched the compiler suite from original MinGW32 to MinGW-W64 and that solved the problem.我将编译器套件从原始 MinGW32 切换到 MinGW-W64 并解决了问题。 I came up with that idea after reading this post:看完这篇文章后,我想到了这个想法:

http://mingw-users.1079350.n2.nabble.com/missing-symbol-ms-vsnprintf-when-linking-with-cross-compiled-library-td7582798.html http://mingw-users.1079350.n2.nabble.com/missing-symbol-ms-vsnprintf-when-linking-with-cross-compiled-library-td7582798.html

Seems the GLFW libs are build with a MinGW64 or MinGW-W64.似乎 GLFW 库是用 MinGW64 或 MinGW-W64 构建的。

MinGW\\include\\stdio.h : MinGW\\include\\stdio.h

/* The following pair ALWAYS refer to the MSVCRT implementations...
 */
_CRTIMP int __cdecl __MINGW_NOTHROW _snprintf (char*, size_t, const char*, ...);
_CRTIMP int __cdecl __MINGW_NOTHROW _vsnprintf (char*, size_t, const char*, __VALIST);
_CRTIMP int __cdecl __MINGW_NOTHROW _vscprintf (const char*, __VALIST);

So just use them with underscore in front of them.所以只需在它们前面使用下划线即可。

I encountered that problem when cross-compiling GMP on Linux for Windos32 with --host=i686-w64-mingw32 .我在使用--host=i686-w64-mingw32在 Linux 上为 Windos32 交叉编译GMP时遇到了这个问题。

As I don't want to mess with GMP's sources or build system, and I have no choice what toolchain to use on Windos32,因为我不想弄乱 GMP 的源代码或构建系统,而且我无法选择在 Windos32 上使用什么工具链,

I figured out the following work-around: When linking, link with我想出了以下解决方法:链接时,链接

-Wl,-u,___mingw_vsnprintf -Wl,--defsym,___ms_vsnprintf=___mingw_vsnprintf

I am preferring a C99 compliant version, anyway.无论如何,我更喜欢 C99 兼容版本。 Notice that that work-around will drag ___mingw_vsnprintf no matter what, ie even in the case the target code does not use vsnprintf .请注意, ___mingw_vsnprintf ,变通方法都会拖拽___mingw_vsnprintf ,即即使在目标代码不使用vsnprintf的情况下。

The mingw version is provided by libmingwex.a ; mingw 版本由libmingwex.a提供; you see that gcc links against it with -Wl,-v which prints -lmingwex (among many others).您会看到 gcc 与-lmingwex -Wl,-v链接-Wl,-v它打印-lmingwex (以及许多其他内容)。

The problem might be that the project's configure has some problems figuring out whether the host's vsnprintf is working properly or not, or whether the user wants to stick with MS stuff or functions that are C99 compliant.问题可能是项目的配置存在一些问题,无法确定主机的vsnprintf是否正常工作,或者用户是否想要坚持使用符合 C99 的 MS 内容或功能。 Anyway, stdio.h of my i686-w64-mingw32 cross-tools as well as the stdio.h on the host have sections that are secured by无论如何, stdio.h的我的i686-W64-mingw32的交叉工具以及在stdio.h的主机上具有由固定部分

#if __USE_MINGW_ANSI_STDIO
/*
 * User has expressed a preference for C99 conformance...
 */
...
#ifdef _GNU_SOURCE

and then define vsnprintf as a wrapper around a call to __mingw_vsnprintf or around a call to __ms_vsnprintf .然后定义vsnprintf作为围绕到调用的包装__mingw_vsnprintf或周围到呼叫__ms_vsnprintf Hence there should also be hacks to the build system and inject -D__USE_MINGW_ANSI_STDIO somewhere.因此,还应该对构建系统进行黑客攻击并在某处注入-D__USE_MINGW_ANSI_STDIO

In the case of autotools, and what worked for me in the case of GMP, is to configure在 autotools 的情况下,在 GMP 的情况下对我有用的是配置

$(srcdir)/configure CPPFLAGS='-D__USE_MINGW_ANSI_STDIO' ...

After re-configure, build and install, nm libgmp.a | grep vsnprintf重新配置、构建和安装后, nm libgmp.a | grep vsnprintf nm libgmp.a | grep vsnprintf shows for the built libraries nm libgmp.a | grep vsnprintf显示构建的库

         U ___mingw_vsnprintf

instead of the previous而不是以前的

         U ___ms_vsnprintf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM