跳转至

--- 标签: - 菲律宾共产党 - VS代码类别: - Linux日期:创建时间:2022-12-30# 更新时间:2024-01-19---

VM CPP QT编译环境与VSCode

1. 前置要求### (1).安装了 Visual Studio Code首先,应在一台运行 Windows/Linux/MacOS 的 PC 上安装最新版本的 VSCode。

其次,应安装以下扩展: [C/C++] [远程SSH] [远程 SSH:编辑配置文件]

(2)。安装了 VMWare Workstation,并配置了虚拟机。

首先,虚拟机要正常运行。 此外,网络也正常(从主机 PING 到虚拟机,反之亦然)。

2. 连接远程环境### (1).配置“SSH 目标”打开“REMOTE EXPLORER”窗口 -> “Add NEW”,然后在对话框中键入以下命令:

ssh USER@192.xxx.xxx.xxx -A 输入密码并将弹出选项保留为默认值。

USER是虚拟机用户名的用户> 192.xxx.xxx.xxx是虚拟机的 IP 地址。 然后选择合适的路径作为编译默认路径,推荐使用Home路径。

(2)。将源代码上传到虚拟机在这一步中,可以应用许多工具,包括scp、sftp、xshell

注意!您应该使用正确的用户上传代码以避免权限错误。

3.VSCode配置步骤1.在VSCode中打开源代码路径。

步骤2。如果不存在这样的目录,请添加一个名为.vscode的新目录。

(1). CPP配置在.vscode文件夹中添加c_cpp_properties.json。内容如下:```

{ "configurations": [ { "name": "RHEL7.2->Linux", "includePath": [ "\({workspaceFolder}/**", "/opt/scadacom/current/**" ], "forcedInclude": [], "defines": [], "compilerPath": "/usr/bin/g++", "cStandard": "c11", "cppStandard": "c++11", "intelliSenseMode": "linux-gcc-x64", "compilerArgs": [ //Release "-std=c++11", "-w", "-O2", "-pipe", "-m64", "-D_REENTRANT", "-DQT_SHARED", "-DQT_NO_DEBUG_OUTPUT", "-DQT_NO_DEBUG", "-DQT_GUI_LIB", "-DQT_NETWORK_LIB", "-DQT_CORE_LIB", "-DQT_SHARED" ] }, { "name": "RHEL7.2-WindowsLocal", "includePath": [ "\)/", "~/Documents/Codes/include/RHEL7.2S5.8.10/", "C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/x86_64-w64-mingw32/include" ], "forcedInclude": [], "defines": [], "compilerPath": "C:/ProgramData/chocolatey/bin/g++.exe", "cStandard": "c11", "cppStandard": "c++11", "intelliSenseMode": "linux-gcc-x64", "compilerArgs": [ //Release "-std=c++11", "-w", "-O2", "-pipe", "-m64", "-D_REENTRANT", "-DQT_SHARED", "-DQT_NO_DEBUG_OUTPUT", "-DQT_NO_DEBUG", "-DQT_GUI_LIB", "-DQT_NETWORK_LIB", "-DQT_CORE_LIB", "-DQT_SHARED" ], "mergeConfigurations": false, "browse": { "path": [ "${workspaceFolder}/", "~/Documents/Codes/include/RHEL7.2S5.8.10/" ], "limitSymbolsToIncludedHeaders": true } } ], "version": 4 }

### (2)。构建任务配置Qmake 和 g++ 编译配置需要在`tasks.json`文件中配置。内容如下:```
{
  "version": "2.0.0",
  // "windows": {
  //   "options": {
  //     "shell": {
  //       "executable": "bash.exe",
  //       "args": ["-c"]
  //     }
  //   }
  // },
  "tasks": [
    {
      "label": "Qmake",
      "type": "shell",
      "command": "/usr/local/Trolltech/Qt-4.8.4/bin/qmake",
      "args": [],
      "group": "build",
      "presentation": {
        "reveal": "silent",
        "clear": true
      }
    },
    {
      "label": "Clean",
      "type": "shell",
      "command": "make",
      "args": [
        "clean"
      ],
      "group": "build",
      "presentation": {
        "reveal": "silent",
        "clear": true
      }
    },
    {
      "label": "MakeCompile",
      "type": "shell",
      "dependsOrder": "sequence",
      "dependsOn": [
        "Clean",
        "Qmake"
      ],
      "command": "/usr/bin/make",
      "args": [
      ],
      "group": "build",
      "presentation": {
        // "reveal": "silent",
        "clear": true
      }
    },
  ]
}
配置任务后,您可以通过按CTRL+Shift+B从 vscode 编辑窗口启动构建任务。 说明书如下:>Qmake: 运行命令/usr/local/Trolltech/Qt-4.8.4/bin/qmake>

Release: 自动运行Qmake后运行命令/usr/bin/make -f Makefile.Release。> Debug: 自动运行Qmake后运行命令/usr/bin/make -f Makefile.Debug。> Clean: 删除 Qmake 和 g++ 生成的文件> Clean-Release: 运行命令make -f Makefile.Release clean> Clean-Debug: 运行命令make -f Makefile.Debug clean

(3)。调试配置GNU gdb 工具将用作调试工具,您可以使用以下launch.json配置来调试代码:```

{ "version": "0.2.0", "configurations": [ { "name": "QT-CPPGDB", "type": "cppdbg", "request": "launch", "program": "\({workspaceFolder}/wg_modbus_ats", "args": [], "stopAtEntry": false, "cwd": "\)/debug/", "environment": [], //"symbolSearchPath": "C:\Symbols", "externalConsole": false, "linux": { "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, "windows": { "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, "osx": { "miDebuggerPath": "/Applications/Xcode.app/Contents/Developer/usr/bin/lldb-mi", "MIMode": "lldb" }, // "logging": { // "moduleLoad": false, // "trace": true // }, "visualizerFile": "my.natvis" }, { "name": "CMake-CPPGDB", "type": "cppdbg", "request": "launch", "program": "\({workspaceFolder}/build/\)", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}/build", "environment": [], //"symbolSearchPath": "C:\Symbols", "externalConsole": false, "linux": { "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, "windows": { "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, "osx": { "miDebuggerPath": "/Applications/Xcode.app/Contents/Developer/usr/bin/lldb-mi", "MIMode": "lldb" }, // "logging": { // "moduleLoad": false, // "trace": true // }, "visualizerFile": "my.natvis" } ] } `` 但是,调试不能自动更改目标,调试流程应如下: 运行CleanClean-Debug-> 运行Debug-> 更改TARGETS-> 设置断点 -> 按F5`。

TARGETS是 g++ 生成的可执行文件。 所有配置文件都可以在[此处]找到。

参考文献[1]. https://blog.dashdreams.com

[2]. [C/C++] [3]. [远程SSH] [4]. [远程 SSH:编辑配置文件] [5]. VSCode-C-CPP-环境 [6]. Qmake-Pro [远程 SSH]:https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh[C/C++]:https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools[远程 SSH:编辑配置文件]:https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh-edit[这里]:https://gitlab.dashdreams.com:8443/Haoran.Qi/include/-/tree/master/.vscode