如何在 Visual Studio 2017 中为 Windows XP 编译代码

Posted

技术标签:

【中文标题】如何在 Visual Studio 2017 中为 Windows XP 编译代码【英文标题】:How to compile code for Windows XP in Visual Studio 2017 【发布时间】:2020-02-13 12:16:07 【问题描述】:

我试图在 Windows 10 上的 Visual Studio 2017 中为 Windows XP 编译一个基本的 hello world 程序。但它给出了一些错误,如图所示。

我已经尝试过有关此问题的其他 Stack Overflow 帖子中提到的步骤,并更改了平台工具“Visual Studio 2017 - Windows XP (v141_xp)”。

#include<iostream>
using namespace std;
int main() 
    cout << "Hello world\n";

1>------ Build started: Project: WindowsProject1, Configuration: Debug Win32 ------
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\Win32\PlatformToolsets\v141_xp\Toolset.targets(39,5): warning MSB8051: Support for targeting Windows XP is deprecated and will not be present in future releases of Visual Studio. Please see https://go.microsoft.com/fwlink/?linkid=2023588 for more information.
1>stdafx.cpp
1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\objbase.h(239): error C2760: syntax error: unexpected token 'identifier', expected 'type specifier'
1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\gdiplusheaders.h(891): error C4596: 'EmfToWmfBits': illegal qualified name in member declaration
1>c:\program files (x86)\microsoft sdks\windows\v7.1a\include\gdiplusstringformat.h(220): error C4596: 'GetTrimming': illegal qualified name in member declaration
1>Done building project "WindowsProject1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

【问题讨论】:

【参考方案1】:

您在这里遇到的是 XP-Compatible SDK 的头文件中的问题。这个问题实际上很神秘,但幸运的是,相对容易处理:您只需要“放松”编译器使用的一致性检查的严格性......

为此,请在解决方案资源管理器中右键单击您的项目,然后选择“属性”。在调用的属性页上,选择“C/C++”选项卡,然后选择“语言”子选项卡。在随后显示的页面中,确保将“一致性模式”选择为“”。这应该可以解决问题。

请随时要求进一步澄清和/或解释。

【讨论】:

但是现在我又遇到了一个问题,编译成功后,当我在windows xp上运行程序时,它显示我缺少一些DLL文件。你能帮我解决这个问题吗? @0xVikas 你几乎肯定需要在目标机器上安装相关的“VC Redistributable”包。这里有一个最近的讨论:***.com/questions/58336827/… 谢谢!我通过将“代码生成 -> 运行时库”更改为多线程调试以静态包含所有 DLL 来修复它。【参考方案2】:

您没有说您开始使用的模板,但我怀疑您启用了一致性模式 (/permissive-)。转到项目 -> 属性 -> C/C++ -> 语言并将一致性模式设置为“否”。

用于支持 Windows XP 的旧 Windows 7.1a SDK 从未更新为符合/permissive-

【讨论】:

同时回答,我猜:我觉得有义务给你一个紫外线! (否则,我在诋毁我自己的答案。?) 非常感谢您的回答!

以上是关于如何在 Visual Studio 2017 中为 Windows XP 编译代码的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 C++ 非托管库在 Visual Studio 2017 中为 Xamarin.Forms 设置项目?

如何在 Visual Studio Code 中为 React Native 设置调试?

在Visual Studio 2017中为WPF应用程序创建MSI安装程序后,EXE不执行任何操作

在 html 中为 Visual Studio 中的 Angular 静态键入

如何在 Visual Studio 2013 中为 sqlite 配置实体框架 6

如何在 Visual Studio Code 中为 Python 设置虚拟环境?