VScode中编写运行C程序
程序员文章站
2024-03-01 18:24:10
...
VScode运行C程序的所需配置
VScode只是一个编辑器,并不自带C编译器,所以需要
下载mingw
下载安装版本或者压缩文件,解压缩后,配置系统的环境变量。
path中添加mingw/bin的路径
新建include变量,添加mingw/include的路径
打开VScode安装c/c++插件和code runner插件
code runner用来一键编译运行C程序。
除此之外然后需要配置三个文件。
c_cpp_properties.json
launch.json
tasks.json
c_cpp_properties.json配置
当鼠标放在include头文件时候会出现一个灯泡,点击选择创建c_cpp_properties.json。
文件中的name选项是根据自己setting.json中的设定选择的。
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "D:\\Software\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "gnu++14"
},
{
"name": "Linux",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"compilerPath": "D:\\Software\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "gnu++14"
},
{
"name": "Win32",
"includePath": [
//电脑中安装的路径
"D:/Software/mingw64/include",
"${workspaceRoot}"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
//电脑中安装的路径
"D:/Software/mingw64/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
//电脑中安装的路径
"compilerPath": "D:\\Software\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "gnu++14"
}
],
"version": 4
}
launch.json配置文件
点击左侧调试按钮可以生成该文件,
targetArchitecture根据自己所需的构架来更改,miDebuggerPath需要按照自己电脑中Mingw-w64的安装目录来更改。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb)c_launch",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x64",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
//替换电脑中的路径
"miDebuggerPath": "D:/Software/mingw64/bin/gdb.exe",
"MIMode": "gdb",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"internalConsoleOptions": "openOnFirstSessionStart",
"externalConsole": true,
"preLaunchTask": "gcc"
}
]
}
tasks.json
快捷键ctrl+shift+p会出现状态栏提示,输入tasks,选择用模板生成文件,然后替换就可以
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "gcc",
"args": ["-Wall", "-g", "${file}", "-o", "${fileBasenameNoExtension}.exe"],
"echoCommand": true,
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
运行C程序,VScode的终端推荐使用默认的powershell,使用git bash会报错找不到c的文件,因此不能在vscode的终端显示。之前使用git bash只能在命令行使用手打命令运行,不知道是不是我配置的关系,更换成powershell就可以运行在终端。
上一篇: vuex面试题
下一篇: java之Object类用法实例