如何使用 WSL 从 Bash 构建现有的 Windows-only Visual Studio 项目
How can I build an existing Windows-only Visual Studio project from Bash using WSL
我有一个已在 Visual Studio 中配置和构建的现有 C++ 项目。该项目的唯一目标是 Windows,没有其他平台。我在 WSL 中使用 Bash 来启动可执行文件。
我更喜欢使用 Visual Code 进行开发(而不是 Visual Studio)。我更喜欢通过 Bash(强大的 Linux 背景)构建和启动应用程序。
现在,我的开发流程是:
- 在 VS Code 中编辑代码
- 切换到 Visual studio 并单击构建按钮
- 切换到Bash并执行构建的程序
因为我只打开 Visual Studio 进行构建,所以我更愿意通过 Bash 通过命令行构建。
我天真的方法是使用开源工具将 Visual Studio 项目文件转换为 CMake 文件。然后 cmake & make from Bash,但是当我开始遇到寻找 windows.h 的错误时我停止了(也许我只需要添加一些 windows 包含路径到我的 include_path)。
我不确定解决此问题的最佳方法是什么。如有任何建议,我们将不胜感激!
如果项目完全是C++,应该没有理由离开WSL。构建和启动应用程序可以在那里轻松处理!
您完全可以使用
在 bash 中通过命令行构建
g++ -o <outputfile> <inputfiles>
但是,运行 程序的最简单方法是在 Visual Code 中创建构建配置。您将需要 2 个文件:launch.json 和 tasks.json
要创建启动文件,请按 F1(或打开您的命令面板)和 select 任务:配置默认构建任务。它应该看起来像这样。
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}", //input files
"-o",
"${fileDirname}/a.out" //output file
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
要创建 launch.json,转到 'debug' 选项卡和 select 'create a launch.json file'。它应该看起来像这样
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/a.out", //output file
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
有了这两个文件,您所要做的就是像 Visual Studio 中那样点击 运行 按钮。
MSBuild.exe 随我安装的 Microsoft Visual Studio 一起提供。在 WSL bash 中,我可以调用 MSBuild.exe 并将我的项目的 .sln 文件作为第一个也是唯一的参数。
编译输出写入终端
我有一个已在 Visual Studio 中配置和构建的现有 C++ 项目。该项目的唯一目标是 Windows,没有其他平台。我在 WSL 中使用 Bash 来启动可执行文件。
我更喜欢使用 Visual Code 进行开发(而不是 Visual Studio)。我更喜欢通过 Bash(强大的 Linux 背景)构建和启动应用程序。
现在,我的开发流程是:
- 在 VS Code 中编辑代码
- 切换到 Visual studio 并单击构建按钮
- 切换到Bash并执行构建的程序
因为我只打开 Visual Studio 进行构建,所以我更愿意通过 Bash 通过命令行构建。
我天真的方法是使用开源工具将 Visual Studio 项目文件转换为 CMake 文件。然后 cmake & make from Bash,但是当我开始遇到寻找 windows.h 的错误时我停止了(也许我只需要添加一些 windows 包含路径到我的 include_path)。
我不确定解决此问题的最佳方法是什么。如有任何建议,我们将不胜感激!
如果项目完全是C++,应该没有理由离开WSL。构建和启动应用程序可以在那里轻松处理! 您完全可以使用
在 bash 中通过命令行构建g++ -o <outputfile> <inputfiles>
但是,运行 程序的最简单方法是在 Visual Code 中创建构建配置。您将需要 2 个文件:launch.json 和 tasks.json 要创建启动文件,请按 F1(或打开您的命令面板)和 select 任务:配置默认构建任务。它应该看起来像这样。
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}", //input files
"-o",
"${fileDirname}/a.out" //output file
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
要创建 launch.json,转到 'debug' 选项卡和 select 'create a launch.json file'。它应该看起来像这样
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/a.out", //output file
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
有了这两个文件,您所要做的就是像 Visual Studio 中那样点击 运行 按钮。
MSBuild.exe 随我安装的 Microsoft Visual Studio 一起提供。在 WSL bash 中,我可以调用 MSBuild.exe 并将我的项目的 .sln 文件作为第一个也是唯一的参数。
编译输出写入终端