Windows on Snapdragon 使用指南(2)
2.3 使用 Visual Studio 2022 构建应用程序
使用 Visual Studio,您有多种选项来构建 ARM64 应用程序:Visual Studio 控制台、Visual Studio IDE 或 Visual Studio 和 NMAKE。
注意:C# 代码可以构建为 ARM64,但不能构建为 ARM64EC。
安装 Visual Studio 2022 时包含以下项目:
- Windows 11 SDK
- 使用 C++ 进行桌面开发
- 通用Windows平台开发
为 ARM64 构建应用程序 (notepad++)
在 Visual Studio 中将项目构建为 ARM64X
ARM64X 是一种新的二进制类型,可以包含经典 ARM64 代码和 ARM64EC 代码。ARM64 或 ARM64EC 配置的属性页具有新属性Build Project as ARM64X ,在项目文件中称为BuildAsX 。
在命令提示符下使用 Visual Studio 批处理文件和 NMAKE 构建应用程序
Microsoft 程序维护实用程序 ( NMAKE.EXE ) 命令行工具包含在 Visual Studio 中。NMAKE 根据 makefile 描述文件中包含的命令构建项目。
2.3.1 为 ARM64 构建应用程序 (notepad++)
- 从 git下载Notepad++代码。
- 在 Visual Studio 中,从C:\Downloads\notepad-plus-1. plus\PowerEditor\visual.net\notepadPlus.sln打开notepadPlus.sln
- 选择解决方案配置 ( Release ) 和解决方案平台 ( ARM64 )。
- 将 Notepad++ 解决方案构建为普通 Visual Studio 项目(选择“构建” > “构建解决方案”)。这还会构建依赖的 Scintilla 和 Lexilla 项目。成功构建会在以下位置生成.exe文件: C:\Downloads\notepad-plus-plus\PowerEditor\binarm64\Notepad++.exe。
您现在可以在计算机上运行notepad++.exe 。
2.3.2 在 Visual Studio 中将项目构建为 ARM64X
ARM64X 是一种新的二进制类型,可以包含经典的 ARM64 代码和 ARM64EC 代码。ARM64 或 ARM64EC 配置的属性页具有新属性Build Project as ARM64X ,在项目文件中称为BuildAsX 。
确保您已完成以下操作:
- 为 ARM64 设备添加新的发布配置
- 为 ARM64EC 设备添加新的发布配置
为了使 BuildAsX 在项目中正常工作,该项目必须具有现有的 ARM64 配置和 ARM64EC 配置。ARM64 和 ARM64EC 配置必须具有相同的 C 运行时和 C++ 标准库。任何直接和间接的项目引用和依赖项也应将 BuildAsX 设置为True。
- 在 Visual Studio 解决方案资源管理器中,右键单击项目节点(解决方案中的项目)并选择“属性”。
- 选择“高级” > “将项目构建为 ARM64X” > “是”。单击 “应用”,然后单击“确定”。
- 选择“构建” > “构建解决方案”。Visual Studio 会将应用程序保存在项目的/ARM64/Release文件夹中。
2.3.3 在命令提示符下使用 Visual Studio 批处理文件和 NMAKE 构建应用程序
Microsoft 程序维护实用程序 ( NMAKE.EXE ) 命令行工具包含在 Visual Studio 中。NMAKE 根据 makefile 描述文件中包含的命令构建项目。
必须使用 Visual Studio 开发人员命令提示符来运行 nmake。开发人员命令提示符设置特定于已安装的工具、平台和 SDK 的环境变量。
-
在 Visual Studio 中,单击“工具” > “命令行” > “开发人员命令提示符”。
-
通过开发人员命令提示符键入set以设置环境变量。
\source\repos\HelloWorld>set
-
将目录更改为开发人员命令提示符快捷方式
-
对于 Visual Studio 企业版: ```c cd C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build ```
- 对于 Visual Studio 专业版:
c cd C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build
-
-
在命令提示符中,输入dir命令以显示命令选项列表,以配置特定构建架构的路径和环境变量。例如:
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build>dir
这些选项取决于计算机上安装的 Visual Studio 工作负载和选项。使用以下命令为特定构建架构配置路径和环境变量:
命令文件 | 主机和目标架构 |
---|---|
vcvarsx86_arm.bat | 使用 32 位 x86 本机工具构建 ARM 代码。 |
vcvarsamd64_arm.bat | 使用 64 位 x64 本机工具构建 ARM 代码。 |
vcvarsx86_arm64.bat | 使用 32 位 x86 本机工具构建 ARM64 代码。 |
vcvarsamd64_arm64.bat | 使用 64 位 x64 本机工具构建 ARM64 代码。 |
vcvarsall.bat | 使用参数指定主机和目标体系结构、Windows SDK 和平台选择。要获取支持的选项列表,请使用 /help参数进行调用。 |
-
运行vcvarsamd64_arm64.bat文件以使用 64 位 x64 原生交叉编译工具构建 ARM64 代码。
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build>vcvarsamd64_arm64.bat
您应该看到以下内容:
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build>vcvarsamd64_arm64.bat ********************************************************************** ** Visual Studio 2022 Developer Command Prompt v17.5.3 ** Copyright (c) 2022 Microsoft Corporation ********************************************************************** [vcvarsall.bat] Environment initialized for: 'x64_arm64'
可以使用以下命令验证编译器cl.exe:
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build>cl.exe Microsoft (R) C/C++ Optimizing Compiler Version 19.35.32216.1 for ARM64 Copyright (C) Microsoft Corporation. All rights reserved. usage: cl [ option... ] filename... [ /link linkoption... ]
-
将目录更改为 HelloWorld 应用程序的位置。
cd C:\Users\%UserProfile%\source\repos\HelloWorld\HelloWorld
-
通过将以下文本保存在名为(无扩展名)的文件中,在与 HelloWorld 应用程序文件相同的目录中创建makefileMakefile :
HelloWorld.exe: HelloWorld.cpp cl HelloWorld.cpp
-
nmake使用应用程序文件和 makefile 目录中的命令构建代码:
C:\Users\%UserProfile%\source\repos\HelloWorld\HelloWorld> nmake
-
要确认 ARM64 二进制文件已生成,请使用以下link /dump /headers 命令来识别 ARM64 二进制文件:
link /dump /headers C:\Users\%UserProfile%\source\repos\HelloWorld\HelloWorld\HelloWorld.exe
例如:
C:\Users\%UserProfile%\source\repos\HelloWorld\HelloWorld>link /dump /headers C:\Users\%UserProfile%\source\repos\HelloWorld\HelloWorld\HelloWorld.exe Microsoft (R) COFF/PE Dumper Version 14.35.32216.1 Copyright (C) Microsoft Corporation. All rights reserved. Dump of file C:\Users\%UserProifile%\source\repos\HelloWorld\HelloWorld\HelloWorld.exe PE signature found File Type: EXECUTABLE IMAGE FILE HEADER VALUES AA64 machine (ARM64)
2.3.3.1 示例:使用 NMAKE 构建应用程序 (7Zip)
-
下载并解压:https://7-zip.org/a/7z2201-src.7z。
-
在 Visual Studio 中,单击“工具” > “命令行” > “开发人员命令提示符”。
-
更改目录:
cd C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\ ```
-
运行vcvarsamd64_arm64.bat文件以使用 64 位 x64-native 交叉工具构建 ARM64 代码。
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build>vcvarsamd64_arm64.bat ********************************************************************** ** Visual Studio 2022 Developer Command Prompt v17.4.5 ** Copyright (c) 2022 Microsoft Corporation ********************************************************************** [vcvarsall.bat] Environment initialized for: 'x64_arm64' C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build>
-
输入以下命令:
cd C:\Users\<userID>\Downloads\7z2201-src\CPP\7zip
-
使用命令构建代码nmake。
例如:C:\Users\<userID>\Downloads\7z2201-src\CPP\7zip>nmake
生成的.exe文件位于以下路径:
C:\Users\<userID>\Downloads\7z2201-src\CPP\7zip\UI\Console\arm64\7z.exe
-
使用以下命令识别 ARM64 二进制文件:
link /dump /headers C:\Users\<userID>\Downloads\7z2201-src\CPP\7zip\UI\Console\arm64\7z.exe
下面的内容就是结果。检查机器类型。现在您可以运行 7z.exe。
Dump of file C:\Users\<userID>\Downloads\7z2201-src\CPP\7zip\UI\Console\arm64\7z.exe PE signature found File Type: EXECUTABLE IMAGE FILE HEADER VALUES AA64 machine(ARM64) 6 number of sections 63DA30B8 time date stamp Wed Feb 1 14:58:24 2023 0 file pointer to symbol table 0 number of symbols F0 size of optional header 22 characteristics Executable Application can handle large (>2GB) addresses OPTIONAL HEADER VALUES 20B magic # (PE32+) 14.34 linker version 60200 size of code 28A00 size of initialized data 0 size of uninitialized data 4D180 entry pint (0000000014004D180)