美文网首页
vscode + cmake编译环境配置

vscode + cmake编译环境配置

作者: ClarkWang1214 | 来源:发表于2021-01-11 17:18 被阅读0次

    对于C++ 工程,有四个必要的json配置文件,先ctrl+shift+p打开输入指令分别是:

    • c_cpp_properties.json :配置项目结构,自动生成和更新,输入C/C++:Edit configuration
    • task.json: 构建和编译运行项目,输入Task:Configure Task,模板,Others
    • launch.json: 调试,读取可执行文件

    1.新建C/C++工程,VScode以文件夹为管理工程的方式,因此需要建立一个文件夹来保存工程。
    2.配置launch.json文件,读取可执行文件。需要进行修改地方的是指定运行的文件,其次我们还可以在里面添加build任务,用于调试。
    3.配置tasks.json文件,这个文件用来方便用户自定义任务,我们可以通过这个文件来添加g++/gcc或者是make命令,方便我们编译程序。
    4.之后就可以进行基础的C/C++开发与调试了。

    这里以《视觉SLAM十四讲》的源码slambook2为例进行演示

    1.加载已有工程

    用VS Code 打开slambook2中的第3个例子ch3中的useGeometry目录

    Screenshot from 2021-01-08 14-50-44.png

    2.新建launch.json文件并更改配置

    launch.json文件用于读取执行输出的文件。

    点击左侧的第四个带有小虫子的按钮,点击选择"create a launch.json file",然后选择“C++(GDB/LLDB)”,然后点击“Default Configuration”默认生成,将自动生成launch.json文件,具体操作如下:


    Screenshot from 2021-01-08 14-57-44.png
    {
        // 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) launch",// 配置名称
                "type": "cppdbg",// 配置类型
                "request": "launch",// 请求配置类型,launch或者attach
                "program": "enter program name, for example ${workspaceFolder}/a.out",// 进行调试程序的路径,程序生成文件.out
                "args": [],// 传递给程序的命令行参数,一般为空
                "stopAtEntry": false,// 调试器是否在目标的入口点停止,
                "cwd": "${workspaceFolder}",// 项目目录
                "environment": [],
                "externalConsole": false,// 调试时是否显示控制台窗口,一般为true显示控制台
                "MIMode": "gdb",// 指定连接的调试器
                "preLaunchTask": "build",//每次调试前会自动编译
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }
    

    需要根据launch.json文件中内容相应修改配置,将其中
    "program": "enter program name, for example ${workspaceFolder}/a.out",
    修改为
    "program": "${workspaceFolder}/build/eigenGeometry",
    其他默认不动

    {
        // 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) Launch",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/build/result",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "preLaunchTask": "build",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }
    

    3.新建编译任务task.json文件并更改配置

    task.json:定义编译方法,转为计算机可识别的语言,生成out文件。

    快捷键ctrl+shift+p打开命令行,输入:Task:Configure Task使用模版创建Tasks.json文件 → Others:

    ctrl+shift+p打开命令行,输入Task:Configure Task 选用others模板
    
    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "echo",
                "type": "shell",
                "command": "echo Hello"
            }
        ]
    }
    

    更改为

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

    其中 "command": "cd ./build ;cmake ../ ;make" 编译命令很关键,会自动执行linux命令

    3配置C++ Edit Configuration

    Ctrl+shift+p打开命令选项,选择C/C++:Edit configuration,自动生成 c_cpp_properties.json配置文件

    自动生成 c_cpp_properties.json配置文件
    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "${workspaceFolder}/**"
                ],
                "defines": [],
                "compilerPath": "/usr/bin/clang",
                "cStandard": "c11",
                "cppStandard": "c++14",
                "intelliSenseMode": "clang-x64",
                "configurationProvider": "ms-vscode.cmake-tools"
            }
        ],
        "version": 4
    }
    

    修改为

    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "${workspaceFolder}/**",
                    "/usr/include",
                    "/usr/include/eigen3",  //程序需要用到的第三方库
                    "/usr/local/include"
                ],
                "defines": [],
                "compilerPath": "/usr/bin/gcc",
                "cStandard": "c11",
                "cppStandard": "c++17",
                "intelliSenseMode": "gcc-x64"
            }
        ],
        "version": 4
    }
    

    4修改CMakeLists.txt文件

    需要在CMakeLists.txt 里加 set (CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -g”) 开启debug 不然断点调试是无效的

    cmake_minimum_required( VERSION 2.8 )
    project( geometry )
    
    #设置debug模式
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
    
    # 添加Eigen头文件
    include_directories( "/usr/include/eigen3" )
    
    add_executable( eigenGeometry eigenGeometry.cpp)
    

    5先编译后调试

    按下ctrl+shift+B执行程序cmake编译,生成eigenGeometry可执行文件后,在代码文件中设置断点,并按F5执行调试

    编译后断点调试

    相关文章

      网友评论

          本文标题:vscode + cmake编译环境配置

          本文链接:https://www.haomeiwen.com/subject/iuhfaktx.html