C++: Unresolved external symbol __sprintf and _sscanf in Visual Studio 2015

手头有一个项目VC++ 6的,非常老了,需要升级到VC++ 2015. 报了一堆的错误Sad smile,这是其中两个:

d3dx8.lib(d3dx8core.obj) : error LNK2019: unresolved external symbol __snprintf referenced in function _D3DXGetErrorStringA@12

d3dx8.lib(cd3dxassembler.obj) : error LNK2001: unresolved external symbol __vsnprintf

从错误看是在尝试Link “d3dx8.lib”,DirectX 8的库,市面上都找不到了……当然我手里还有lib文件,所以问题不在这Smile(是的,我是C++新手)

Google一番之后发现原来这是VS2015的一个坑,参考MSDN的文章 Breaking Changes in Visual C++ 2015

The printf and scanf family of functions are now defined inline.

The definitions of all of the printf and scanf functions have been moved inline into <stdio.h>, <conio.h>, and other CRT headers. This is a breaking change that leads to a linker error (LNK2019, unresolved external symbol) for any programs that declared these functions locally without including the appropriate CRT headers. If possible, you should update the code to include the CRT headers (that is, add #include <stdio.h>) and the inline functions, but if you do not want to modify your code to include these header files, an alternative solution is to add an additional library to your linker input, legacy_stdio_definitions.lib.

To add this library to your linker input in the IDE, open the context menu for the project node, choose Properties, then in the Project Properties dialog box, choose Linker, and edit the Linker Input to add legacy_stdio_definitions.lib to the semi-colon-separated list.

If your project links with static libraries that were compiled with a release of Visual C++ earlier than 2015, the linker might report an unresolved external symbol. These errors might reference internal stdio definitions for _iob, _iob_func, or related imports for certain stdio functions in the form of _imp_*. Microsoft recommends that you recompile all static libraries with the latest version of the Visual C++ compiler and libraries when you upgrade a project. If the library is a third-party library for which source is not available, you should either request an updated binary from the third party or encapsulate your usage of that library into a separate DLL that you compile with the older version of the Visual C++ compiler and libraries.

System_CAPS_warningWarning

If you are linking with Windows SDK 8.1 or earlier, you might encounter these unresolved external symbol errors. In that case, you should resolve the error by adding legacy_stdio_definitions.lib to the linker input as described previously.

VC++ 2015对printf和scanf相关的方法做了inline的处理,导致一些旧的库出现LINK2019的错误。开发者可以选择修改代码包含<stdio.h>等头文件并重新编译代码,或者将“legacy_stdio_definition.lib”添加到”“Linker->Input->Additional Dependencies”。

对于我来说,DirectX 8没法重新编译代码,只能选择后者了。

最后,感谢StackOverFlow的帮助http://stackoverflow.com/questions/32418766/c-unresolved-external-symbol-sprintf-and-sscanf-in-visual-studio-2015 这篇文章中提到的错误跟我的略有不同—— _sprintf vs __sprintf ——导致我并没有第一时间Google到它。

posted @ 2016-08-05 15:48  排骨虾  阅读(2911)  评论(0编辑  收藏  举报