Home > Software design >  How to compile C app for Windows XP in MSVS?
How to compile C app for Windows XP in MSVS?

Time:12-11

As I read this article, it is enough to download most recent MSVS 2022 and then install toolset C Windows XP Support for VS 2017 (v141) tools [Deprecated].

After that in Visual Studio inside project properties I set this toolset. According to linked article it is enough to compile C app with XP support.

But after my .exe file is created if I run it on XP 64-bit SP2 then it shows error that CompareStringEx function is not found in KERNEL32.DLL.

Hence it appears that it is not enough to use this toolset. Something else is needed.

In some other places I see that one needs also to add define /D_USING_V110_SDK71_ when compiling and option /SUBSYSTEM:CONSOLE,5.01 when linking. In my project properties I also tried to add this two options, but still CompareStringEx is inside import table of final application.

As suggested by @BenVoigt, I did defines /DWINVER=0x0502 /D_WIN32_WINNT=0x0502. Also set C standard to /std:c 14 (I would set C 11 but this MSVS version allows to set only C 14 at minimum). Still some non-XP symbols remain in final EXE like InitializeSRWLock that is possibly used by C 11's std::mutex in my code.

Does anyone know everything what is needed in order to compile fully XP-compatible application?

Update. I managed to build working XP application by doing things above plus setting C CRT runtime to Multi Threaded DLL, i.e. using dynamic DLL linkage of CRT. Also as suggested by @ChuckWalbourn, I downloaded older version of msvcp140.dll.

But it is very important for my project to have statically linked runtime (C CRT), i.e. use Multi Threaded value for Runtime field in project properties. Only if it is REALLY not possible only then I will use DLL CRT. Until then solution about how to link CRT statically are welcome, of course to produce XP-compatible EXE.

CodePudding user response:

TL;DR For Window XP VC REDIST support, install https://aka.ms/vs/15/release/VC_redist.x86.exe on your Windows XP system

-or-

if you are doing "side-by-side application local deployment", then use the DLLs from C:\Program Files\Microsoft Visual Studio\2022\<edition>\VC\Redist\MSVC\14.16.27012\x86\Microsoft.VC141.CRT.

If you want the latest bug fixes to the CRT, you can also download the REDIST for VS 2019 (16.7) per the link on Microsoft Docs.

For Windows XP targeting, you use the v141_xp Platform Toolset installed by Visual Studio (VS 2017, VS 2019, or VS 2022) which is the latest VS 2017 (v141) C compiler using an included Windows 7.1A SDK.

Make sure you have installed (for VS 2022) the following individual components since you are using MFC:

  • Microsoft.VisualStudio.Component.WinXP: C Windows XP Support for VS 2017 (v141) tools [Deprecated]
  • Microsoft.VisualStudio.Component.VC.v141.x86.x64: MSVC v141 - VS 2017 C x64/x86 build tools (v14.16)
  • Microsoft.VisualStudio.Component.VC.v141.MFC: C MFC for v141 build tools (x86 & x64)

If you are doing DirectX development, be sure to read this blog post as well for various implications of using the Windows 7.1A SDK.

For deployment to Windows XP, you can install the latest VS 2017 Visual C REDIST or use VS 2019 Visual C up to VS 2019 (16.7). After that the REDIST DLLs themselves are not compatible with Windows XP.

On your development system with VS 2022 installed, you are going to have a newer set of Visual C REDIST files which are binary compatible with your v141_xp Platform Toolset built EXE, but those VC REDIST DLLs are not compatible with Windows XP.

IOW: If you look at a dumpbin /imports of the 14.30 (v143 version), 14.29 (v142 latest version), and/or 14.16 (v141 latest version ) copies of msvcp140.dll you will see different imports. The msvcp140.dll sitting in your C:\windows\SysWOW64 folder is going to be the 14.30 version.

  • Related