[C++] Configuration du fichier CMake de VSCode sous Windows

Utiliser VSCode pour développer des programmes C/C++ de haut niveau nécessite généralement une configuration tasks.jsonet launch.jsonces deux fichiers

task.json : fichier de configuration de construction du compilateur ;
launch.json : fichier de configuration de configuration du débogueur ;

1. Compilation de fichiers uniques

Compilez d'abord le fichierg++ -g main.cpp -o main.exe

1.1 Créer un fichier de configuration de construction du compilateur tâches.json

Sélectionnez "Terminal → Configurer les tâches" dans la barre de menu pour générer, la configuration est la suivante (faites principalement attention à l'emplacement du chemin g++)
insérer la description de l'image ici

{
    
    
	"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 Créer un fichier de configuration de construction du compilateur launch.json

insérer la description de l'image ici
insérer la description de l'image ici

La configuration pertinente est la suivante :

{
    
    
    // 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. Compilation multi-fichiers

Compilez d'abord le fichierg++ -g main.cpp swap.cpp -o mutil_swap

2.1 Modifier la configuration de launch.json

Modifiez principalement "program"les paramètres et commentez"preLaunchTask"

La configuration pertinente est la suivante :

{
    
    
    // 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 compile directement

3.1 CréerCMakelists.txt

project(MYSWAP) # 工程名

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

3.2 Autre
ctrl + shift + pentrée de configuration CMakeSélectionnez CMake: Configure, puis sélectionnez Compilateur
insérer la description de l'image ici
insérer la description de l'image ici
pour terminer le processus ci-dessus et générer un buildfichier

3.3 Générerexe

Ouvrez le terminal et suivez les étapes pour entrer

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

insérer la description de l'image ici
À ce stade, buildle dossier apparaîtmy_cmake_swap.exe

3.4 Modifier launch.jsonles paramètres suivants dans

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

Cela peut également être réalisé en utilisant
insérer la description de l'image ici

4. CMake compile à l'aide de la configuration task.json

En task.jsonconfiguration directe CMake, dependsOnle paramètre correspond à cmakel'exécution cmake .. , et makel'exécution estmingw32-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"
            ]
        }
    ],

}

Correspondant à la modification launch.json, la modification est principalement liée "program"au "preLaunchTask"paramètre

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

Je suppose que tu aimes

Origine blog.csdn.net/weixin_42166222/article/details/127511586
conseillé
Classement