63

For a research project, I'm writing a C++ add-on to a scientific computing language. Unfortunately the library that allows users to do this is not kept very well up-to-date.

I started the project in XCode, where it built fine. Later I had to move to a PC, so I migrated the code to Visual Studio 2015. Since doing this, I haven't been able to build due to the following errors:

LNK2001 : unresolved external symbol _sprintf
LNK2019 : unresolved external symbol _sscanf referenced in function _GetDDouble
LNK2019 : unresolved external symbol _sprintf referenced in function _CheckRunningInMainThread

An attempted fix was to add the header #define _CRT_SECURE_NO_WARNINGS. However, this a) fixed no errors and b) added the warning C4005 : '_CRT_SECURE_NO_WARNINGS': macro redefinition. I assume the library already defined this macro, anticipating this problem. Regardless, it didn't solve the problem.

How should I proceed?

3 Answers 3

190

Add the following library to the linker input files:

legacy_stdio_definitions.lib

VS 2015 now uses inline definitions that call internal functions for many of the stdio.h functions. If an object file (or library member) depends on one of those functions, then the legacy_stdio_definitions.lib provides an externally linkable version of the function that can be linked to.

Your other option is to recompile the unit that depends on those functions with VS 2015 (this is probably the preferred option).

8
  • @user4581301: can you elaborate? Sep 6, 2015 at 2:38
  • 1
    Sorry. Supporting, even if with a wrapper, the old stdio stuff without wailing and moaning. Every now and then snprintf is exactly the right tool for the job, and I hate having to do portability wrappers for something that should just come in the box. Sep 6, 2015 at 4:55
  • @Epirocks: I haven't tested that myself yet - are you using the lib from C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib\amd64? What are some of the exact error messages you get? Nov 16, 2015 at 16:08
  • 1
    No, x64 but in the end the errors were coming from a separate library which has been replaced by the vendor, so disregard what I said, thanks.
    – Epirocks
    Nov 18, 2015 at 10:46
  • 3
    You could also add: #pragma comment (lib, "legacy_stdio_definitions.lib") into one of the source files to have it added by the linker as well.
    – Aria
    Jun 21, 2020 at 23:30
3

I got this error compiling cycling max plugins against version 5 max sdk (pure c api). The legacy library fix didn't work for me (it should have, and if anyone had any idea why it mightn't I'd be curious), but I defined _NO_CRT_STDIO_INLINE before stdio was loaded and that did do the trick.

0
0

I recently encountered this and was able to add User32.lib to Linker > Input > Additional Dependencies.

You could also include #pragma comment (lib, "User32.lib") in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.