

















VSCode官网:Visual Studio Code - Code Editing. Redefined
C/C++扩展:

选择MinGW(Minimalist GNU on Windows)
安装:
版本信息选择:
这里直接下载 Online Installer安装会报错。用下面的具体版本安装。

直接编译.c文件:gcc -g hello_world_c.c -o hello_world_c.exe
-g:仅编译,生成调试信息-o:指定编译得到的文件名(默认为a.out)在VSCode菜单栏,选择【终端】->【配置任务...】,选择【C/C++: gcc.exe 生成活动文件】
则会自动在工作区文件夹生成.vscode文件夹下的tasks.json文件:
"-fdiagnostics-color=always",行"label":表示这个任务的名称(后面在运行配置launch.json中会用到)"command":任务的执行文件gcc.exe解释器的路径"args":执行文件后跟的参数gcc.exe -g ${file} -o ${fileDirname}\\${fileBasenameNoExtension}.exe{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"args": [
//"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe"
}
]
}在VSCode菜单栏,选择【运行】->【添加配置...】,选择【C++ (GDB/LLDB)】
在页面右下方点击【添加配置】->【C/C++:(gdb) 启动】

配置生成的launch.json文件
"program":要运行的可执行程序"externalConsole":是否需要外置的Console"miDebuggerPath":设置dbg.exe路径"preLaunchTask":运行前执行任务"Label"值){
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件"
}
]
}完成配置后,就可以直接编译运行或者调试了。
直接编译:g++ -g hello_world_cpp.cpp -o hello_world_cpp.exe
多个task和configuration可以分别写在同一个tasks.json和launch.json文件中。
tasks.json文件
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"
}
],
"version": "2.0.0"
}launch.json文件
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}有文件main.c,hello.c和hello.h
main.c文件#include "hello.h"
int main()
{
printHello();
return 0;
}hello.h#ifndef __HELLO_H__
#define __HELLO_H__
#include <stdio.h>
void printHello();
#endifhello.c#include "hello.h"
void printHello()
{
printf("Hello world!\n");
}对以上多个文件进行直接编译:
gcc *.c -o main.exegcc hello.c main.c -o main.exe对目录下所有的.c文件进行编译
.h头文件,将其代码拷贝到.c文件中此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。