【问题标题】:How can I build an existing Windows-only Visual Studio project from Bash using WSL如何使用 WSL 从 Bash 构建现有的仅限 Windows 的 Visual Studio 项目
【发布时间】:2020-03-11 22:11:02
【问题描述】:

我有一个在 Visual Studio 中配置和构建的现有 C++ 项目。这个项目的唯一目标是 Windows,没有其他平台。我在 WSL 中使用 Bash 来启动可执行文件。

我更喜欢使用 Visual Code(而不是 Visual Studio)进行开发。我更喜欢通过 Bash(强大的 Linux 背景)构建和启动应用程序。

现在,我的开发工作流程是:

  1. 在 VS Code 中编辑代码
  2. 切换到 Visual Studio 并单击构建按钮
  3. 切换到 Bash 并执行构建的程序

由于我只打开 Visual Studio 进行构建,我更喜欢通过 Bash 使用命令行构建。

我天真的方法是使用开源工具将 Visual Studio 项目文件转换为 CMake 文件。然后从 Bash 进行 cmake & make,但是当我开始遇到寻找 windows.h 的错误时我停止了(也许我只需要添加一些 windows 包含路径到我的 include_path)。

我不确定最好的方法是什么。任何建议将不胜感激!

【问题讨论】:

    标签: visual-studio windows-subsystem-for-linux


    【解决方案1】:

    如果项目完全是 C++,应该没有理由离开 WSL。构建和启动应用程序可以在这里轻松处理! 您完全可以使用

    在 bash 中通过命令行构建

    g++ -o <outputfile> <inputfiles>

    但是,运行程序最简单的方法是在 Visual Code 中创建构建配置。您将需要 2 个文件:launch.json 和 tasks.json 要创建启动文件,请按 F1(或打开命令托盘)并选择任务:配置默认构建任务。它应该看起来像这样。

    {
        "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,请转到“调试”选项卡并选择“创建launch.json 文件”。它应该看起来像这样

    {
        // 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 中一样点击运行按钮。

    【讨论】:

    • 谢谢!不过,该项目确实需要为 Windows 构建。
    【解决方案2】:

    MSBuild.exe 随我安装的 Microsoft Visual Studio 一起提供。在 WSL bash 中,我可以调用 MSBuild.exe 并将我的项目的 .sln 文件作为第一个也是唯一的参数。

    编译输出写入终端。

    【讨论】:

    • 当我从 Ubuntu WSL 尝试这个时,我得到了MSBuild.exe: command not found,你需要做一些特别的事情吗?
    • @Richie 是的。 MSBuild.exe 存在于 Visual Studio 目录中。您可以将此目录添加到您的 bash 路径中,也可以创建一个使用确切路径的别名。
    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 2011-04-08
    • 2019-07-02
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多