ubuntu上VSCode构建cmake工程

4.各种json文件配置

打开一个含有CMakeLists.txt的完整的Cmake工程文件夹
在.vscode文件夹中要建立三个json文件才能对Cmake工程进行编译和调试,分别是c_cpp_properties.json,launch.json和tasks.json
4.1 c_cpp_properties.json的配置

ctrl+shift+p→C/Cpp: Edit Configurations(JSON),生成c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}

4.2 launch.json配置

按F5,生成launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/application",  //输入程序名称,例如 ${workspaceFolder}/a.out
            "args": ["test1", "test2"],  //输入程序运行参数
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/build",  //设置程序运行初始路径
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

4.3 tasks.json配置

ctrl+shift+B→生成tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make build",//编译的项目名,build
            "type": "shell",
            "command": "cd ./build ;cmake ../ ;make",//编译命令
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "make clean",
        }
    ]
}

5. 编写CMakeLists.txt

例如:

cmake_minimum_required(VERSION 2.8)

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g -ggdb")
add_compile_options(-std=c++11)

set(source ./block.h ./graph.h ./graph.cpp ./maxflow.cpp ./main.cpp)

add_executable(maxflow ${source})

6.执行

ctrl+shift+B 编译程序
F5 执行程序


7.调试

在CMakeLists.txt文件中添加

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g -ggdb")

即可通过断点进行调试
一个通过cmake组织的工程通过以上步骤可以变成一个可debug的vscode工程

原文链接:https://blog.csdn.net/yinwenbin0805/article/details/103945958

猜你喜欢

转载自blog.csdn.net/weixin_45834800/article/details/130724595