【C++】Windows下VSCode的CMake文件配置

使用VSCode开发高度C/C++程序,一般需要配置 tasks.jsonlaunch.json 这2个文件

tasks.json: 编译器构建 配置文件 ;
launch.json: 调试器设置 配置文件 ;

1、单文件编译

先编译文件 g++ -g main.cpp -o main.exe

1.1 创建 tasks.json 编译器构建配置文件

菜单栏选择 " Terminal → Configure Tasks" 即可生成,配置如下(主要注意g++路径位置)
在这里插入图片描述

{
    
    
	"version": "2.0.0",
	"tasks": [
		{
    
    
			"type": "cppbuild",
			"label": "C/C++: g++.exe build active file",
			"command": "F:\\mingw64\\bin\\g++.exe",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
    
    
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
    
    
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: F:\\mingw64\\bin\\g++.exe"
		}
	]
}

1.2 创建 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) Launch", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg", // 配置类型,这里只能为cppdbg  
            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", //将要进行调试的程序的路径  
            "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],
            "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台====用true的时候需要在return的上面加getchar();  
            "MIMode": "gdb",

            "miDebuggerPath": "F:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "setupCommands": [
                {
    
    
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
        }
    ]
}

2、多文件编译

先编译文件 g++ -g main.cpp swap.cpp -o mutil_swap

2.1 修改launch.json配置

主要对"program"参数进行修改,并注释掉 "preLaunchTask"

相关配置如下:

{
    
    
    // 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", // 配置类型,这里只能为cppdbg  
            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${fileDirname}/mutil_swap.exe", //将要进行调试的程序的路径  
            "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],
            "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台====用true的时候需要在return的上面加getchar();  
            "MIMode": "gdb",

            "miDebuggerPath": "F:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "setupCommands": [
                {
    
    
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            // "preLaunchTask": "C/C++: g++.exe build active file", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc 
        }
    ]
}

3、CMake直接编译

3.1 创建 CMakelists.txt

project(MYSWAP) # 工程名

add_executable(my_cmake_swap main.cpp swap.cpp) # 生成的cmake.exe文件, 文件组成

3.2 其他配置
ctrl + shift + p 输入 CMake 选择 CMake: Configure,然后选择编译器
在这里插入图片描述
在这里插入图片描述
结束上述流程会生成一个 build 文件件

3.3 生成 exe

打开终端,按步骤输入

cd .\build\
cmake ..
mingw32-cmake.exe

在这里插入图片描述
此时,build 文件夹下就出现了 my_cmake_swap.exe

3.4 修改 launch.json 中下列参数即可

"program": "${fileDirname}/build/my_cmake_swap.exe", //将要进行调试的程序的路径  
// "preLaunchTask": "C/C++: g++.exe build active file", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc 

也可使用以下方式实现
在这里插入图片描述

4、CMake 使用 task.json 配置编译

使用 task.json直接配置 CMakedependsOn 参数对应的 cmake 执行的就是 cmake .. make 执行的就是mingw32-make.exe

{
    
       
    "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", //windows,linux 命令为 make
            "args": [

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

}

修改对应的 launch.json,改动主要是 "program""preLaunchTask" 参数

{
    
    
    // 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", // 配置类型,这里只能为cppdbg  
            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceFolder}/build/my_cmake_swap.exe", //将要进行调试的程序的路径  
            "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],
            "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台====用true的时候需要在return的上面加getchar();  
            "MIMode": "gdb",
            "miDebuggerPath": "F:/mingw64/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "setupCommands": [
                {
    
    
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Build", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/weixin_42166222/article/details/127511586