cmake学习

add_subdirectory(子文件夹名)表示对子文件夹项目进行cmake编译

set(CMAKE_VERBOSE_MAKEFILE on) 是否显示编译信息
set(CMAKE_CXX_FLAGS
“${CMAKE_CXX_FLAGS} -g -std=c++17 -pipe -O2 -Werror -Wall -Wno-sign-compare -Wno-unused-parameter -Wno-deprecated-declarations -fopenmp -fPIC -pthread -Wl,–copy-dt-needed-entries”
)

  • 编译设置说明:
    -g: 调试模式,添加后可使用gdb等调试工具
    -O2 :编译优化,可能导致断点调试时目标代码执行顺序改变
    -Werror :warning作为error输出
    -Wall :warning输出

cmake和vsode调试环境

1、lauch.json负责的是启动任务,执行文件(可执行文件)
2、tasks.json负责的是配置相关任务。简单来说就是负责编译链接生成可执行文件,其实就是执行终端的编译指令[g++ -g main.cpp -o main.o]。所以在执行launch.json文件之前必须先执行tasks.json
3、launch.json和tasks.json通过launch.json中的preLaunchTask关联起来。launch.json中的preLaunchTask是为了启动tasks.json的,所以preLaunchTask对应的标签应该与task.json一致。

参数介绍

launch.json

{
    "version": "2,0.0", //配置文件的版本,以前使用是0.2.0,新版本已经弃用,改用为2.0.0
    "configurations": [ //配置域
        {
            "name": "(gdb) Launch", //配置文件的名字
            "type": "cppdbg", //调试的类型,这是cpp
            "request": "launch",//配置文件的请求类型,有launch和attach两种
            "targetArchitecture": "x64", //硬件内核架构,为64bit
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.out",//将要进行调试的可执行文件的路径和文件名称
            "args": [],//主函数调用时传入的参数,一般为空
            "stopAtEntry": false,//设为true时程序将暂停在程序入口处,一般设为false
            "cwd": "${workspaceFolder}",//调试时的工作目录
            "environment": [],
            "internalConsoleOptions": "openOnSessionStart",//
            "externalConsole": true,//调试时是否显示控制台窗口,一般设置为true
            "MIMode": "gdb",//指定连接的调试器,可以省略不写
            //"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",//调试器路径,在Linux环境下需要注释掉这一行
            "setupCommands": [ 
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",//调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应,一般为编译程序,c++为g++, c为gcc,采用cmake的多文件编译则为build
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "label": "Build", // 任务名称,与launch.json的preLaunchTask相对应
    "version": "2.0.0",
    "command": "g++",// cpp使用g++命令,c使用gcc
    "args": [ 
        "-g",
        "${file}", 
        "-o", 
        "${fileDirname}/${fileBasenameNoExtension}.out", 
        "-std=c++11",    //使用c++11
        "-lpthread"      //链接thread库
    ],
    //${fileDirname} 当前打开的文件所在的绝对路径,不包括文件名
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation":  ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}
folder 介绍

${workspaceRoot} 当前打开的文件夹的绝对路径+文件夹的名字
${workspaceFolder}当前程序的路径,.vscode路径
${file}当前打开正在编辑的文件名,包括绝对路径,文件名,文件后缀名
${fileBasename} 当前打开的文件名+后缀名,不包括路径
${fileBasenameNoExtension} 当前打开的文件的文件名,不包括路径和后缀名
${fileDirname} 当前打开的文件所在的绝对路径,不包括文件名
${fileExtname} 当前打开的文件的后缀名

cmake+vscode设置
{
    // 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 ./src/build ;cmake ../ ;make",//编译命令,更改
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "make clean",
        }
    ]
}

参考文章:
https://blog.youkuaiyun.com/sinat_38816924/article/details/124238534
https://blog.youkuaiyun.com/misterdo/article/details/120381680

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值