Skip to content

VSCode C/CPP Environment

1. Install MinGW

具体参考C++ 环境设置

2. Configure VSCode

在源代码文件夹下创建一个.vscode文件夹,里面包含c_cpp_properties.json,launch.json,tasks.json,各文件的具体含义可以参考VScode tasks.json和launch.json的设置VSCode预定义变量

(1). c_cpp_properties.json

需要将MinGW路径更改为实际路径

{
    "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). launch.json

需要将MinGW路径更改为实际路径

{
  "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"
    }
  ]
}

(3). 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
      }
    },
  ]
}

REF

[1]. https://www.runoob.com/cplusplus/cpp-environment-setup.html

[2]. https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables

[3]. https://zhuanlan.zhihu.com/p/92175757

[4]. https://www.cnblogs.com/bpf-1024/p/11597000.html

[5]. https://zhuanlan.zhihu.com/p/87864677