[C++] CMake file configuration of VSCode under Windows

Using VSCode to develop high-level C/C++ programs generally requires configuration tasks.jsonand launch.jsonthese two files

tasks.json : compiler build configuration file;
launch.json : debugger configuration configuration file;

1. Single file compilation

Compile the file firstg++ -g main.cpp -o main.exe

1.1 Create tasks.json compiler build configuration file

Select "Terminal → Configure Tasks" in the menu bar to generate, the configuration is as follows (mainly pay attention to the g++ path location)
insert image description here

{
    
    
	"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 Create launch.json compiler build configuration file

insert image description here
insert image description here

The relevant configuration is as follows:

{
    
    
    // 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. Multi-file compilation

Compile the file firstg++ -g main.cpp swap.cpp -o mutil_swap

2.1 Modify launch.json configuration

Mainly "program"modify the parameters and comment out"preLaunchTask"

The relevant configuration is as follows:

{
    
    
    // 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 compiles directly

3.1 CreateCMakelists.txt

project(MYSWAP) # 工程名

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

3.2 Other Configuration
ctrl + shift + pInput CMakeSelect CMake: Configure, and then select Compiler
insert image description here
insert image description here
to end the above process and generate a buildfile

3.3 Generateexe

Open the terminal and follow the steps to enter

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

insert image description here
At this point, buildthe folder appearsmy_cmake_swap.exe

3.4 Modify launch.jsonthe following parameters in

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

It can also be achieved by using
insert image description here

4. CMake compiles using task.json configuration

Using task.jsondirect configuration CMake, dependsOnthe parameter corresponds to cmakethe execution cmake .. , and makethe execution ismingw32-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"
            ]
        }
    ],

}

Corresponding to the modification launch.json, the modification is mainly related "program"to "preLaunchTask"the parameter

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

Guess you like

Origin blog.csdn.net/weixin_42166222/article/details/127511586