VSCode, CMake配置(刷题向,运行多个main函数,多文件)

提醒: 仅供参考,是本人记录用的。
参考:
使用CLion 刷题解决多个main函数问题的终极方法
手把手教会VSCode的C++环境搭建,多文件编译,Cmake,json调试配置( Windows篇)

将launch.json和tasks.json放入vscode文件夹,CMakeLists.txt与源文件放同一目录。然后配置cmake为gcc生成build文件夹。

launch.json文件

打开VSCode,点击自动生成launch.json文件。【CTRL+SHITF+D
(若生成的launch.json文件只有两行,将C/C++插件降级至1.8.4)
launch.json文件主要修改programcwd以及preLaunchTask 行的内容

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\build\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\Software_Installation\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build"
        }
    ]
}

CMakeLists.txt文件

复制以下代码。

优点:方便省时

缺点:这种方法要求所有cpp文件命名不重复,不能含有中文,不能含有‘/’等字符!因为它就是直接Copy你的源码文件名的。

file (GLOB_RECURSE files *.c)
foreach (file ${files})
string(REGEX REPLACE ".+/(.+)\\..*" "\\1" exe ${file})
add_executable (${exe} ${file})
message (\ \ \ \ --\ src/${exe}.cpp\ will\ be\ compiled\ to\ bin/${exe})
endforeach ()

tasks.json文件

复制以下代码。

{   
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build"
    },
    "tasks": [
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                ".."
            ],
        },
        {
            "label": "make",
            "group": {
                "kind": "build", 
                "isDefault": true
            },
            "command": "mingw32-make.exe",
            "args": [

            ],
        },
        {
            "label": "Build",
            "dependsOn":[
                "cmake",
                "make"
            ]
        }
    ],

}

你可能感兴趣的:(VSCode零碎笔记,vscode,c++,ide)