1.新建一个文件夹,作为vscode代码的存储文件夹
2.在vscode中打开此文件夹,并新建一个.c文件
3.按下F5,选择顶部的C++(GDB/LLDB),系统会自动生成.vscode文件夹
4.配置.vscode文件夹下的文件。
4.1首先是launch.json,文件内容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "build",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "F:/MinGW/bin/gdb.exe",//为mingw安装路径下bin中的gdb.exe的绝对路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}]
}
4.2 tasks.json文件,自动生成的不需要改动
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
"\"${file}\"",
"--std=c++11",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
]
}
}
]
}
4.3 c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "F:\\MinGW\\bin\\gcc.exe",//为mingw安装路径中bin下的gcc.exe的绝对路径
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "windows-gcc-x86"
}
],
"version": 4
}
5.简单测试,在新建的.c文件中输入代码:
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("abc");
system("pause");
return 0;
}
程序可以运行,但是会有如下报错提示:
Loaded ‘C:\Windows\SysWOW64\kernel32.dll’. Symbols loaded.
Loaded ‘C:\Windows\SysWOW64\KernelBase.dll’. Symbols loaded.
Loaded ‘C:\Windows\SysWOW64\msvcrt.dll’. Symbols loaded.
Loaded ‘C:\MinGW\bin\libgcc_s_dw2-1.dll’. Symbols loaded.
Loaded ‘C:\MinGW\bin\libstdc+±6.dll’. Symbols loaded.
[New Thread 4936.0xf00]
[New Thread 4936.0x1e48]
[New Thread 4936.0x2504]
The program ‘e:\vscode\test.exe’ has exited with code -1 (0xffffffff).
解决此问题,可以在return 0;前加上getchar( );,完整程序如下:
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("abc");
system("pause");
getchar();
return 0;
}
好啦,vscode就可以用来编译C代码啦!
参考博客:
https://my.oschina.net/u/3669041/blog/1838710
https://blog.csdn.net/weixin_43026197/article/details/102101834
本文介绍了如何在VSCode中配置和使用C++环境进行C程序开发。通过新建文件夹、创建.C文件、配置launch.json以及解决运行时报错问题,详细展示了VSCode的C编程流程。
6153





