侧边栏壁纸
博主头像
G

  • 累计撰写 85 篇文章
  • 累计创建 48 个标签
  • 累计收到 10 条评论

目 录CONTENT

文章目录

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

G
G
2022-06-29 / 0 评论 / 0 点赞 / 936 阅读 / 3,225 字 / 正在检测是否收录...

安装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
0

评论区