Search code examples
visual-studiovisual-c++static-libraries

MSVC 2019: libc externals in legacy library not resolved


I am porting a legacy app (originally compiled with VC 6.0) to MSVC 2019. It uses a legacy static library (libtest.lib), compiled with VC 6.0. My problem is that some (but not all) standard libc functions in libtest.lib are not being resolved by MSVC 2019.

For example, libtest.lib uses vsprintf(). On compilation MSVC 2019 reports "unresolved external symbol _vsprintf referenced in ...". Other libc functions (e.g. strlen) are resolved just fine.

I've tried mucking with a gazillion build settings with no joy.
Why is this happening and how can I fix it?

My research so far...

I did a lot of digging in the VC60 headers and found that vsprintf is defined in studio.h as

_CRTIMP int __cdecl vsprintf(char *,const char *, va_list);

while strlen in defined in string.h as

size_t  __cdecl strlen(const char *); 

I'm not sure of the implications but I'm guessing that strlen is just a plain-vanilla static function while vsprintf is imported from some CRT DLL (which is probably different in MSVC 2019).

I guess I could port libtest.lib to MSVC 2019 too, but I've got close to 100 other static libraries and porting them all would be a major pain in the butt.


Solution

  • Thanks to @dewaffled for this great answer:
    Microsoft has a library to adapt CRT functions to the latest versions of MSVC e.g., 2019

    Simply add the following line to your main program:

    #pragma comment(lib, "legacy_stdio_definitions.lib")
    

    Alternatively, I think you can add the library to:

    Project Properties -> Linker -> Input -> Additional Dependencies