Visual Studio - X11:缺少 DISPLAY 环境变量

Visual Studio - X11: The DISPLAY environment variable is missing

我正在开发跨平台 (Windows / Linux) x64 GUI 应用程序,使用 Visual Studio 2019 Enterprise。

在这个 2019 版中,我们可以使用 Visual Studio 来 调试两个平台(Windows - Native)和 Linux - Ubuntu(运行在虚拟机中)。

你可以在这里看到: https://devblogs.microsoft.com/cppblog/using-visual-studio-for-cross-platform-c-development-targeting-windows-and-linux/

所以,我遵循了本教程,并使用 Visual Studio 2019 IDE 在我的 Ubuntu 18 VM 中获得了这个 GUI 示例应用程序 运行 宁和调试。 完美!

现在,我尝试做我的自定义 GUI 应用程序:

为了做一个跨平台的 GUI,我使用的是 GLFW 3.3。

步骤:

  1. 创建一个 Visual Studio CMake 项目

  2. 编写C++代码和CMakeLists.txt

  3. 添加 VS 调试配置(Windows x64 和 Linux x64)

  4. 编译和 Link( VS Windows 和 Linux 目标上都正常)

  5. 运行( VS Windows 和 Linux 目标上都正常)

但是...

当我在 Windows 设置中 运行 时,一切正常...GLFW window 出现...很好! 当我 运行 它在 Linux-debug 中(通过 Visual Studio),当 VS 调试器命中 glfwInit() 行时,我得到这个错误:

Starting GLFW context, OpenGL 3.3

Glfw Error 65544: X11: The DISPLAY environment variable is missing

所以,当我 select Linux 调试配置 我的 VM 中没有 GUI Window。

在网上搜索,我看到有必要说 Visual Studio 导出 launch.vs.json 文件中的 DISPLAY linux 环境变量。

你可以在这里看到: https://docs.microsoft.com/en-us/cpp/build/get-started-linux-cmake?view=vs-2019

在我的 VM Ubuntu 中,我得到了 DISPLAY:0

然后,我写的launch.vs.json文件:

"export DISPLAY=:0;${debuggerCommand}"

"export DISPLAY=:0.0;${debuggerCommand}"

详情:

如果我 手动 进入我的 Ubuntu 虚拟机,然后双击已编译的应用程序,它会显示 GLFW window 和 一切顺利!

我的问题是:

我如何使用 Visual Studio 2019 IDE 将 DISPLAY 环境变量导出到 Linux VM 以调试将 运行 在 Virtual Box(VM)中的应用程序).

Microsoft C++ 团队的一些好人(感谢 Ion、Erika 和 Elisabeth)帮助我,我找到了解决方案。

问题与 Visual Studio 自动生成的 "launch.json" 文件有关。

什么 "launch.json" 属性 我必须更改:

1-设置一个"name": "helloworld"。默认值为 " ".

2- 设置 "project": "CMakeLists.txt"。默认值为 " ".

3- 设置 "projectTarget": "helloworld"。这个属性是不是由VS 2019自动创建的

4- 设置 "cwd": "${debugInfo.defaultWorkingDirectory}"。默认值为 "${debugInfo.defaultRemoteDirectory}"

5- 在 pipeArgs

中添加 "export DISPLAY=:0;"

6- 删除行 "processId: 0"。使用此行,只有 root 用户 可以在 Linux.

上部署和调试

7- 在 pipeArgs 中添加新行:"--tty=${debugInfo.tty}"。这行是不是在你创建CMake Project VS2019

时自动生成的

所以 pipeArgs 是:

"pipeArgs": [
          "/s",
          "${debugInfo.remoteMachineId}",
          "/p",
          "${debugInfo.parentProcessId}",
          "/c",
          "export DISPLAY=:0;${debuggerCommand}",
          "--tty=${debugInfo.tty}"
        ]