VSCode中编译调试CMake组织的C++项目

安装MinGW编译器

详细教程请百度搜索,安装之后将MinGW的bin目录添加到Path环境变量。

编写CMakeLists.txt

  • VSCode插件 CMake、CMake Tools
cmake_minimum_required(VERSION 3.2)
 
if(CMAKE_VERSION VERSION_LESS 3.0.0)
    include(CheckCXXCompilerFlag)
    check_cxx_compiler_flag(-std=c++11 COMPILER_SUPPORTS_CXX11)
    check_cxx_compiler_flag(-std=c++0x COMPILER_SUPPORTS_CXX0X)
    if(COMPILER_SUPPORTS_CXX11)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    elseif(COMPILER_SUPPORTS_CXX0X)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
    endif()
else()
    SET(CMAKE_CXX_STANDARD 11)
    SET(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

project(vscode_cmake)
set(SRC_LIST main.cpp)
add_executable(test ${SRC_LIST})

编写tasks.json

下面的操作依次为
cd ${workspaceFolder}/build
cmake .. -G "MinGW Makefiles"
make

{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type": "shell",
            "label": "clean",
            "command": "del",
            "args": [
                "./*",
                "-r"
            ]
        },
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                "..",
                "-G",
                "MinGW Makefiles"
            ],
            "dependsOn":[
                "clean"
            ]
        },
        {
            "type": "shell",
            "label": "cmake_debug",
            "command": "cmake",
            "args": [
                "..",
                "-G",
                "MinGW Makefiles",
                "-DCMAKE_BUILD_TYPE=Debug"
            ],
            "dependsOn":[
                "clean"
            ]
        },
        {
            "label": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command":"mingw32-make",
            "args": [
                "-j4"
            ],
            "dependsOn":[
                "cmake"
            ]
        },
        {
            "label": "make_debug",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "command":"mingw32-make",
            "args": [
                "-j4"
            ],
            "dependsOn":[
                "cmake_debug"
            ]
        }
    ]
}

编写launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "run",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/test.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "make"
        },
        {
            "name": "debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/test.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "make_debug"
        }
    ]
}

其他

  • 注释插件 koroFileHeader
# CMake  C++  VSCode 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×