手頭有一個項目VC++ 6的,非常老了,需要升級到VC++ 2015. 報了一堆的錯誤,這是其中兩個:
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文件,所以問題不在這(是的,我是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.
Warning
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到它。