VSCode & CMake & vcpkg 整合

发布时间 2023-08-07 16:19:01作者: Cynhard85

VSCode 整合 CMake

调试 CMake 工程

// launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            // Resolved by CMake Tools:
            "program": "${command:cmake.launchTargetPath}",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    // add the directory where our target was built to the PATHs
                    // it gets resolved by CMake Tools:
                    "name": "PATH",
                    "value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}"
                }
            ],
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

传递 CMake Configure 参数

// settings.json
{
    "cmake.configureArgs": [
        "-DHELLOCMAKE=ON"
    ]
}

设置 CMake 生成目录

// settings.json
{
    "cmake.buildDirectory": "${workspaceFolder}/build_${buildKit}/${buildType}"
}

CMake 整合 vcpkg

  • VSCode 中配置 CMake 使用的工具链为 vcpkg :

    // settings.json
    {
        "cmake.configureSettings": {
            "CMAKE_TOOLCHAIN_FILE": "[vcpkg root]/scripts/buildsystems/vcpkg.cmake"
        }
    }
    
  • CMakeLists.txt 同目录下增加 vcpkg.json:

    // vcpkg.json
    {
        "dependencies": [
            "fmt"
        ]
    }