visual studio编译linux,在 Visual Studio 中配置 Linux MSBuild C++ 项目

在 Visual Studio 中配置 Linux MSBuild C++ 项目Configure a Linux MSBuild C++ project in Visual Studio

10/16/2020

本文内容

Linux 支持在 Visual Studio 2017 及更高版本中提供。Linux support is available in Visual Studio 2017 and later.

This topic describes how to configure a MSBuild-based Linux project as described in Create a Linux MSBuild C++ project in Visual Studio. 对于 Linux CMake 项目,请参阅配置 Linux CMake 项目。

可以将 Linux 项目配置为以物理 Linux 计算机、虚拟机或适用于 Linux 的 Windows 子系统 (WSL) 为目标。You can configure a Linux project to target a physical Linux machine, a virtual machine, or the Windows Subsystem for Linux (WSL).

Visual Studio 2019 版本 16.1:Visual Studio 2019 version 16.1 :

面向 WSL 时,可免去生成和获取 IntelliSense 时所需的复制操作,这些操作是面向远程 Linux 系统时所需的。When you target WSL, you can avoid the copy operations needed to build and get IntelliSense that are required when you target a remote Linux system.

可以指定用于生成和调试的独立 Linux 目标。You can specify separate Linux targets for building and debugging.

常规设置General settings

若要查看配置选项,请选择“项目”>“属性”菜单,或在“解决方案资源管理器”中右键单击相应项目,然后从上下文菜单中选择“属性” 。To view configuration options, select the Project > Properties menu, or right-click on the project in Solution Explorer and select Properties from the context menu. 显示“常规”设置。The General settings appear.

3ce5dd50b150fc17efdc32771cdb6adf.png

默认情况下,生成可执行文件 (.out)。By default, an executable (.out) is built. 若要生成静态或动态库,或使用现有生成文件,请使用“配置类型”设置。To build a static or dynamic library, or to use an existing Makefile, use the Configuration Type setting.

如果要针对适用于 Linux 的 Windows 子系统 (WSL) 进行构建,则 WSL 版本 1 限制为 64 个并行编译进程。If you're building for Windows Subsystem for Linux (WSL), WSL Version 1 is limited to 64 parallel compilation processes. 这由“配置属性”>“C/C++”>“常规”中的“最大并行编译作业数”设置控制 。This is governed by the Max Parallel Compilation Jobs setting in Configuration properties > C/C++ > General.

无论使用的 WSL 版本是什么,如果打算使用超过 64 个并行编译进程,建议使用 Ninja 进行构建,这通常会更快且更可靠。Regardless of the WSL version you are using, if you intend to use more than 64 parallel compilation processes, we recommend that you build with Ninja--which generally will be faster and more reliable. 要使用 Ninja 进行构建,请使用“配置属性”>“常规”中的“启用增量生成”设置 。To build with Ninja, use the Enable Incremental Build setting in Configuration properties > General.

有关属性页中设置的详细信息,请参阅 Linux 项目属性页参考。For more information about the settings in the property pages, see Linux Project Property Page Reference.

远程设置Remote settings

要更改与远程 Linux 计算机相关的设置,请配置常规下显示的远程设置。To change settings related to the remote Linux computer, configure the remote settings that appear under General.

要指定远程目标 Linux 计算机,请使用“远程生成计算机”条目。To specify a remote target Linux computer, use the Remote Build Machine entry. 这可以让你选择之前创建的连接之一。This will allow you to select one of the connections created previously.

bbba1c6b3ffa6bf3605a5d75bbe7718a.png

Visual Studio 2019 版本 16.7:若要面向适用于 Linux 的 Windows 子系统 (WSL),请将“平台工具集”下拉列表设置为“适用于 Linux 的 Windows 子系统的 GCC” 。Visual Studio 2019 version 16.7 : To target Windows Subsystem for Linux (WSL), set the Platform Toolset drop-down to GCC for Windows Subsystem for Linux. 其他远程选项将消失,默认 WSL shell 的路径将显示在其位置:The other remote options will disappear and the path to the default WSL shell will appear in their place:

fb543d4b2de99cd047dda288ebb14c98.png

如果进行了并行 WSL 安装,则可在此处指定其他路径。If you have side-by-side WSL installations, you can specify a different path here. For more information about managing multiple distros, see Manage and configure Windows Subsystem for Linux.

可在“配置属性”>“调试”页面上指定用于调试的其他目标 。You can specify a different target for debugging on the Configuration Properties > Debugging page.

“远程生成根目录”确定在远程 Linux 计算机上生成项目的根位置。The Remote Build Root Directory determines the root location of where the project is built on the remote Linux computer. 除非更改,否则该位置默认为 ~/projects 。This will default to ~/projects unless changed.

“远程生成项目目录”是在远程 Linux 计算机上生成此特定项目的位置。The Remote Build Project Directory is where this specific project will be built on the remote Linux computer. 该位置默认为 $(RemoteRootDir)/$(ProjectName) ,它将扩展到以当前项目命名的目录,在上面设置的根目录下。This will default to $(RemoteRootDir)/$(ProjectName) , which will expand to a directory named after the current project, under the root directory set above.

备注

要更改默认的 C 和 C++ 编译器,或者用于生成项目的链接器和存档程序,请使用“C/C++”>“常规”部分和“链接器”>“常规”部分中的相应条目 。To change the default C and C++ compilers, or the Linker and Archiver used to build the project, use the appropriate entries in the C/C++ > General section and the Linker > General section. 例如,可以指定某个版本的 GCC 或 Clang。You can specify a certain version of GCC or Clang, for example.

复制源(仅限远程系统)Copy sources (remote systems only)

面向 WSL 时不可使用此部分。This section doesn't apply when targeting WSL.

在远程系统上进行生成时,开发电脑上的源文件将复制到 Linux 计算机并在该计算机上进行编译。When building on remote systems, the source files on your development PC are copied to the Linux computer and compiled there. 默认情况下,Visual Studio 项目中的所有源文件都将复制到上述设置中设置的位置。By default, all sources in the Visual Studio project are copied to the locations set in the settings above. 但是,也可以将其他源文件添加到此列表,或完全关闭复制源文件,这是生成文件项目的默认值。However, additional sources can also be added to the list, or copying sources can be turned off entirely, which is the default for a Makefile project.

“ 要复制的源 ”确定将哪些源文件复制到远程计算机。Sources to copy determines which sources are copied to the remote computer. 默认情况下,@(SourcesToCopyRemotely) 默认为项目中的所有源代码文件,但不包含图像等任何资产/资源文件。By default, the @(SourcesToCopyRemotely) defaults to all source code files in the project, but doesn't include any asset/resource files, such as images.

可以打开和关闭“ 复制源 ”,以启用和禁用将源文件复制到远程计算机操作。Copy sources can be turned on and off to enable and disable the copying of source files to the remote computer.

通过“要复制的其他源”,可添加将复制到远程系统的其他源文件。Additional sources to copy allows you to add additional source files, which will be copied to the remote system. 可以指定以分号分隔的列表,也可以使用 := 语法指定要使用的本地和远程名称:You can specify a semi-colon delimited list, or you can use the := syntax to specify a local and remote name to use:

C:\Projects\ConsoleApplication1\MyFile.cpp:=~/projects/ConsoleApplication1/ADifferentName.cpp;C:\Projects\ConsoleApplication1\MyFile2.cpp:=~/projects/ConsoleApplication1/ADifferentName2.cpp;

生成事件Build events

由于所有编译都是在远程计算机(或 WSL)上进行的,因此,已将数个其他生成事件添加到“项目属性”中的“生成事件”部分。Since all compilation is happening on a remote computer (or WSL), several additional Build Events have been added to the Build Events section in Project Properties. 它们是“远程预生成事件”、“远程预链接事件”和“远程后期生成事件”,将在此过程中的各个步骤之前或之后在远程计算机上发生 。These are Remote Pre-Build Event , Remote Pre-Link Event , and Remote Post-Build Event , and will occur on the remote computer before or after the individual steps in the process.

0b234374a342f6a04c97c3f3e1b5ad8f.png

远程系统上标头的 IntelliSenseIntelliSense for headers on remote systems

在连接管理器中添加新连接时,Visual Studio 会自动为远程系统上的编译器检测包含目录。When you add a new connection in Connection Manager , Visual Studio automatically detects the include directories for the compiler on the remote system. 随后 Visual Studio 压缩这些文件压缩,并将其复制到本地 Windows 计算机上的目录中。Visual Studio then zips up and copies those files to a directory on your local Windows machine. 此后,每当在 Visual Studio 或 CMake 项目中使用该连接时,都会使用这些目录中的标头来提供 IntelliSense。After that, whenever you use that connection in a Visual Studio or CMake project, the headers in those directories are used to provide IntelliSense.

备注

远程标头副本已在 Visual Studio 2019 版本 16.5 及更高版本中得到优化。In Visual Studio 2019 version 16.5 and later, the remote header copy has been optimized. 现在打开 Linux 项目或为 Linux 目标配置 CMake 时,会按需复制标头。Headers are now copied on-demand when opening a Linux project or configuring CMake for a Linux target. 根据项目指定的编译器,按每个项目在后台进行复制。The copy occurs in the background on a per-project basis, based on the project's specified compilers.

此功能取决于 Linux 计算机是否安装了 zip。This functionality depends on the Linux machine having zip installed. 可使用此 apt-get 命令安装 zip:You can install zip by using this apt-get command:

sudo apt install zip

若要管理标头缓存,导航到“工具”>“选项”,依次选择“跨平台”>“连接管理器”>“远程标头 IntelliSense 管理器”。To manage your header cache, navigate to Tools > Options, Cross Platform > Connection Manager > Remote Headers IntelliSense Manager. 若要在更改 Linux 计算机后更新标头缓存,请选择远程连接,然后选择“更新”。To update the header cache after making changes on your Linux machine, select the remote connection and then select Update. 选择“删除”,来删除标头但不删除连接本身。Select Delete to remove the headers without deleting the connection itself. 选择“浏览”,打开“文件资源管理器”中的本地目录 。Select Explore to open the local directory in File Explorer. 将此文件夹视为只读文件夹。Treat this folder as read-only. 要下载在 Visual Studio 2017 版本 15.3 之前创建的现有连接的标头,请选择该连接,然后选择“下载”。To download headers for an existing connection that was created before Visual Studio 2017 version 15.3, select the connection and then select Download.

c822915181ced72cdaa551c347ba23a3.png

bd6e586d9917ea61d0e1d3236c9be210.png

可以启用日志记录以帮助解决问题:You can enable logging to help troubleshoot problems:

79ca609452fe6d1087d3c901078dbe55.png

Linux 目标区域设置Linux target locale

Visual Studio 语言设置不会传播到 Linux 目标,因为 Visual Studio 不会管理和配置已安装的包。Visual Studio language settings aren't propagated to Linux targets because Visual Studio doesn't manage or configure installed packages. “输出”窗口中显示是消息(例如生成错误)是采用 Linux 目标的语言的区域设置显示的。Messages shown in the Output window, such as build errors, are shown using the language and locale of the Linux target. 你需要为所需区域设置配置 Linux 目标。You'll need to configure your Linux targets for the desired locale.

请参阅See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值