Skip to content

VM CPP QT Compiling Environment with VSCode

1. Pre-requirements

(1). With Visual Studio Code installed

First of all, the latest version of VSCode should be installed on one PC running Windows/Linux/MacOS.

Secondly, the following Extensions should be installed:

C/C++

Remote-SSH

Remote-SSH:Editing-Configuration-Files

(2). With VMWare Workstation installed including a Virtual machine configured.

Firstly, the virtual machine should run properly.

Also, the network goes OK (PING from host to the VMs, and the reverse).

2. Connect the Remote Environment

(1). Configure a "SSH TARGETS"

Open "REMOTE EXPLORER" window -> "Add NEW", and type the following command in the dialog:

ssh USER@192.xxx.xxx.xxx -A

Type in the password and leave the popup options default.

USER is the user of VMs' username

192.xxx.xxx.xxx is the IP address of the VM.

Then choose a proper path as the compiling default path, the Home path is recommended.

(2). Upload the Source Code to the virtual machine

In this step, many tools can be applyed, including scp、sftp、xshell.

ATTENTION! You should upload the code with a proper user to avoid the permission error.

3.VSCode Configuration

Step1. Open the path of source code in VSCode.

Step2. Add a new directory named .vscode if no such directory exist.

(1). Cpp Configuration

Add c_cpp_properties.json in the .vscode folder. The content as follow:

{
    "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": [
                "${workspaceFolder}/**",
                "~/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). Building Task Configuration

Qmake and g++ compile configuration need configured in a tasks.json file. The content as follow:

{
  "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
      }
    },
  ]
}
After task configured, you can launch build task from vscode edit window by press CTRL+Shift+B.

The manual as follow:

Qmake: runing command /usr/local/Trolltech/Qt-4.8.4/bin/qmake

Release: running command /usr/bin/make -f Makefile.Release after running Qmake automatically.

Debug: running command /usr/bin/make -f Makefile.Debug after running Qmake automatically.

Clean: delete files generate by Qmake and g++

Clean-Release: running command make -f Makefile.Release clean

Clean-Debug: running command make -f Makefile.Debug clean

(3). Debug Configuration

GNU gdb tool will be used as the debug tool, you can debug the code with such launch.json configuration:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "QT-CPPGDB",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/wg_modbus_ats",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}/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/${workspaceFolderBasename}", 
      "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"
    }
  ]
}
However, the debug can not change the target automatically, the debug flow should be as follow:

Running Clean or Clean-Debug -> Running Debug -> Change TARGETS -> Set BreakPoint -> press F5.

The TARGETS is the executable file generate by g++.

All Configuration files can be found Here.

REF

[1]. https://blog.dashdreams.com

[2]. C/C++

[3]. Remote-SSH

[4]. Remote-SSH:Editing-Configuration-Files