简体   繁体   中英

C++: error LNK2019: unresolved external symbol __snprintf referenced in function

I'm trying to compile a project but I'm getting this error:

1>project.obj : error LNK2019: unresolved external symbol __snprintf referenced in function "unsigned char * __cdecl GetResource(char *,unsigned long &)" (?GetResource@@YAPAEPADAAK@Z)
1>Release/file.bin : fatal error LNK1120: 1 unresolved externals

I'm using VC++ 2010 and also tried compiling with CodeBlocks, but getting a similar error:

windres.exe  -J rc -O coff -i C:\Users\x\Desktop\project\project\File.rc -o .objs\File.res
windres.exe: no resources
Process terminated with status 1 (0 minutes, 0 seconds)

I have File.rc in my project directory but it's 0kb, I have no idea how to fix this.

I'm pretty sure this is not related to something in the code so I did not post a sample, but if you require one, I'll post it.

Linker errors are generated due to how c++ and many other similar languages compile and link code. The compiler generates object files (*.obj) and only requires function declarations in order to compile code that calls these functions- that's why forward declarations and header files work. After compilation, at link time, the linker looks for the definitions of these functions in compiled files (usually in object files (*.obj) or libraries (*.lib). A linker error saying "Unresolved external symbol" means a declaration without matching definition has been used. Most often this is because of a spelling error in the declaration or definition, or because the file containing the definition was never linked.

Long story short, you are missing a reference to a library. That might be libcmt.lib for release and libcmtd.lib for debug.

I know this post is old so this is my on answer to this problem in case someone has same issue

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 , , 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 ) 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.

The other answer is right (a missing reference). However, in my case (using VS2013) an issue occurred because snprintf did not have underscore in front of it. I added the underscore (made the call to _snprintf), and in other case added "#define snprintf _snprintf" in a header. It resolved the linking issue. HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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