MSVC 2019: libc externals in legacy library not resolved - visual-studio

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.

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

Related

Unresolved external symbol __vsnprintf .... (in dxerr.lib)?

I am running a DirectX 11 application on windows 7 and visual studio community 2015 RC. I'm still using functions from the DX SDK. It worked fine on VS2013 but when I switched over I get only the following error:
Error LNK2019 unresolved external symbol __vsnprintf referenced in function "long __stdcall StringVPrintfWorkerA(char *,unsigned int,unsigned int *,char const *,char *)" (?StringVPrintfWorkerA##YGJPADIPAIPBD0#Z) Ancora D:\Moody\Moody\Projects\Projects\Ancora\Ancora\dxerr.lib(dxerra.obj) 1
I only use the DXGetErrorDescriptionA function from the dxerr library and when I comment it out, the program compiles fine. I have no idea what's wrong but it can't be from the DX SDK or otherwise the other functions would fail right?
I experienced the same problem using DXGetErrorMessage() with Dx9 and found out that MS have provided an additional library to include in the Additional Dependencies properties page to address this problem. The library name is: legacy_stdio_definitions.lib
Adding this resolved the issue for me.
just add
#pragma comment(lib, "legacy_stdio_definitions.lib")
https://msdn.microsoft.com/en-us/library/bb531344.aspx
Instead of hacking dxerr.lib manually, you could add
#include <Windows.h>
#include <stdio.h>
int (WINAPIV * __vsnprintf)(char *, size_t, const char*, va_list) = _vsnprintf;
somewhere in your code
The legacy DirectX SDK is quite old, and dxerr.lib in the DXSDK is not compatible with VS 2015's C Runtime as you have encountered.
In general static libraries with code in them don't mix well from different versions of the compiler. Most of the .libs in the legacy DirectX SDK work with VS 2015 because they are import libraries for dlls or all data libraries and therefore contain no code at all. The DXSDK has not been updated since VS 2010.
Aside: The VS team has made a heroic effort to keep the Visual C/C++ Runtime link compatible between VS 2015 Update 3, VS 2017, and VS 2019 per Microsoft Docs. This is not the normal pattern.
Be sure to read the instructions on Microsoft Docs on the proper way to mix the legacy DirectX SDK with the Windows 8.x SDK used by VS 2015. You are presumably using something else from the legacy DirectX SDK in this project besides dxerr.
I have implemented a version of DXERR that you can build from source in your project to remove this dependency of the legacy DirectX SDK. See this post for details. That said, I purposely only supported Unicode (the W version). You can work out how to make the ANSI (the A version) easily enough, but it would be best if updated your app to use Unicode.
See Where is the DirectX SDK (2021 Edition)? and DXUT for Direct3D 11.
UPDATE: As noted in another answer linking with legacy_stdio_definitions.lib should make the old legacy DirectX SDK version of dxerr.lib link again with VS 2015/2017. That said, you should work on removing dependencies on the legacy DirectX SDK as much as possible and DXERR is easily replaced by your own module. See Living without D3DX.
The DirectX libraries you are using are compiled with an older version of Visual Studio than you are using. Microsoft sometimes makes changes to their C runtime, creating incompatibilities between libraries compiled with different versions. __vsnprintf was an internal symbol in older versions of their C runtime, it does not exist in the 2015 RC version.
Unfortunately, dxerr.lib (along with d3dx11.lib) have been deprecated. You have two options - you can switch back to VS2013 or you can stop using functionality from dxerr.lib. The latter is probably better, because you can duplicate its functionality by using FormatMessage now (more info in the linked article).
HACKY but you could patch dxerr.lib.
Replace __vsnprintf with _vsnprintf (with a null at the end to account for the removed underscore at the beginning)
You can change the Platform Toolset from Visual Studio 2015 to Visual Studio 2013 and then it compiles.
The Platform Toolset is found on the General tab of the Project Properties.

Qt, VC 2010 and static libraries

I need to use a static library in my Qt5 project. I'm using VC++ 2010 as my compiler and QtCreator as IDE.
If I use the dynamic version (.DLL) everything works fine.
If I try to use the static version (.LIB) it seems like the library is looking for other dependencies that can't be satisfied.
Sadly, using the DLL is not an option for my project.
The point is that if I try to use the static library from VC++ IDE everything works fine.
Please note that I can use other VC++ headers without any problem in my Qt project.
It seems like the compiler can find everything it needs (basically, my source code and all the required headers) while the linker is missing something.
error: LNK2019: unresolved external symbol _imp_SystemTimeToVariantTime#8 referenced in function "bool __cdecl SystemTimeToMinute(struct _SYSTEMTIME *,unsigned long *)" (?SystemTimeToMinute##YA_NPAU_SYSTEMTIME##PAK#Z)
SystemTimeToVariantTime is defined in OleAut32.lib.
Am I wrong expecting the linker to automatically look for needed libraries and headers based on the content of the global variables INCLUDE and LIB?
Solved.
The problem was that I was trying to include external (SDK) libraries using
LIBS += -l OleAut32.lib
While I had to simply use
LIBS += OleAut32.lib
Well, problem solved.
I hope this can help someone in the future!

How to include the math library in MS-VS 2005 compiler project settings?

I am trying to build a C code which originally is built on linux with
gcc -lm ... option, which uses the math library while linking the code. How do I use the same in project settings of a Visual Studio 2005 compiler, on Win32 environment, to link the math library. Will just saying
#include "math.h"
be enough or i need to give math library as a dependency in the VS-2005 project settings, If yes then what is the setting for that?
Basic problem is when in my windows build, I include math.h but I get a compilation error: nanf():- identifier not found
I am looking to resolve this.
thank you,
-AD
#include "math.h" works fine on VS2005.
See Floating-Point Support.

Linker error with debug build with different compiler versions

We have a DLL, built with MS Visual Studio 2010, in release mode. We provide this DLL to different customers, along with a .lib file. The functions in the DLL are exported with:
extern "C" __declspec(dllexport) int analyze(int id);
Our customers have two applications that makes use of this DLL. Both of these applications import the DLL functions using:
extern "C" __declspec(dllimport) int analyze(int id);
One of these applications is built with MS Visual Studio 2010. This application can be built successfully in both debug and release modes.
The other application must unfortunately use MS Visual Studio 2005 as its build environment. In this application, the release build can be built successfully, however, when we try to build in debug mode, we get linker errors:
LNK2019: unresolved external symbol __imp_analyze referenced in function "void __cdecl process(char const *,char const *)" (?process##ABCERFG0#Z)
Can someone help me understand what we are missing here? Are we exporting the functions in a manner that is not portable across compilers? What's the solution?
Regards,
The .obj file format is highly conserved between VS2005 and VS2010. This should not be a problem, especially since it is a simple non-mangled symbol reference. And especially not when it works in the Release configuration but not in Debug. A simple explanation is always better than a convoluted one: your customer simply forgot to add your .lib file to the linker's Additional Dependencies setting.
Beware that the setting change needs to be made for both configurations, use the "Configuration" combo in the upper left corner of the dialog.
You can help your customer fall into the pit of success by using #pragma comment(lib, "mumble.lib") in your .h file.

Using GLUT with Visual C++ Express Edition

What are the basic steps to compile an OpenGL application using GLUT (OpenGL Utility Toolkit) under Visual C++ Express Edition?
If you don't have Visual C++ Express Edition (VCEE), download and install VCEE.
The default install of Visual C++ Express Edition builds for the .Net platform. We'll need to build for the Windows platform since OpenGL and GLUT are not yet fully supported under .Net. For this we need the Microsoft Platform SDK. (If you're using an older version of VCEE, download and install the Microsoft Platform SDK. Visual C++ Express Edition will need to be configured to build for Windows platform. All these instructions are available here.)
If you don't have GLUT, download and unzip Nate Robin's Windows port of GLUT.
Add glut.h to your Platform SDK/include/GL/ directory
Link the project with glut.lib. (Go to VCEE Project Properties -> Additional Linker Directories and add the directory which has glut.lib.
Add glut.dll to the Windows/System32 directory, so that all programs using GLUT
can find it at runtime.
Your program which uses GLUT or OpenGL should compile under Visual C++ Express Edition now.
The GLUT port on Nate Robin's site is from 2001 and has some incompatibilities with versions of Visual Studio more recent than that (.NET 2003 and up). The incompatibility manifests itself as errors about redefinition of exit(). If you see this error, there are two possible solutions:
Replace the exit() prototype in glut.h with the one in your stdlib.h so that they match. This is probably the best solution.
An easier solution is to #define GLUT_DISABLE_ATEXIT_HACK before you #include <gl/glut.h> in your program.
(Due credit: I originally saw this advice on the TAMU help desk website.)
I've been using approach #1 myself since .NET 2003 came out, and have used the same modified glut.h with VC++ 2003, VC++ 2005 and VC++ 2008.
Here's the diff for the glut.h I use which does #1 (but in appropriate #ifdef blocks so that it still works with older versions of Visual Studio):
--- c:\naterobbins\glut.h 2000-12-13 00:22:52.000000000 +0900
+++ c:\updated\glut.h 2006-05-23 11:06:10.000000000 +0900
## -143,7 +143,12 ##
#if defined(_WIN32)
# ifndef GLUT_BUILDING_LIB
-extern _CRTIMP void __cdecl exit(int);
+/* extern _CRTIMP void __cdecl exit(int); /* Changed for .NET */
+# if _MSC_VER >= 1200
+extern _CRTIMP __declspec(noreturn) void __cdecl exit(int);
+# else
+extern _CRTIMP void __cdecl exit(int);
+# endif
# endif
#else
/* non-Win32 case. */

Resources