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

6

我正在尝试在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 ==========

2个回答

11
您所遇到的问题是XP-Compatible SDK头文件中的问题。问题实际上相当复杂,但幸运的是,相对容易处理:您只需要“放松”编译器使用的一致性检查的严格性……
要做到这一点,在解决方案资源管理器中右键单击项目,然后选择“属性”。在调用的属性页上,选择“C/C++”选项卡,然后选择“语言”子选项卡。在随后显示的页面中,请确保选择“一致性模式”为“”。这应该可以解决问题。
如有进一步需要澄清和/或解释,请随时提问。

但是现在我面临另一个问题,编译成功后,在Windows XP上运行程序时,它显示一些DLL文件丢失。你能帮我修复这个问题吗? - 0xVikas
@0xVikas 你几乎肯定需要在目标机器上安装相关的“VC Redistributable”软件包。这里最近有关于此的讨论:https://stackoverflow.com/questions/58336827/how-to-run-a-c-program-i-wrote-on-another-computer/58336900#58336900 - Adrian Mole
2
谢谢!我通过将“代码生成->运行时库”更改为多线程调试,以静态方式包含所有DLL来解决了这个问题。 - 0xVikas

3

您没有说明使用的模板,但我怀疑您启用了符合性模式 (/permissive-)。请前往 项目 -> 属性 -> C/C++ -> 语言,将符合性模式设置为“否”。

旧的 Windows 7.1a SDK 用于支持 Windows XP,从未更新为与 /permissive- 兼容。


1
同时回答,我猜:我感到有责任给你一个UV!(否则,我会贬低自己的回答。?) - Adrian Mole
非常感谢您的回答! - 0xVikas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接