Visual Studio Code C语言开发环境搭建
文章目录
- Visual Studio Code C语言开发环境搭建
- 一、Visual Studio Code安装与主题配置
- 1、Visual Studio Code安装
- 2、配置中文主题
- 3、配置字体颜色主题
- 4、配置文件图标主题
- 二、安装C / C ++扩展。
- 三、编译与调试配置
- 1、创建工作空间
- 2、创建配置文件
- 2.1 c_cpp_properties.json文件
- 2.2 tasks.json 文件
- 2.3 launch.json 文件
一、Visual Studio Code安装与主题配置
1、Visual Studio Code安装
Visual Studio Code安装程序下载地址: https://code.visualstudio.com/
2、配置中文主题
1)打开扩展(Ctrl+Shift+X)输入 Language
搜索 Chinese (Simplified) Language Pack for Visual Studio Code
点击安装
2)打开命令面板(Ctrl+Shift+P ),输入Configure Display Language
选择zh-cn
,软件重新启动切换到中文界面
3、配置字体颜色主题
- 打开扩展(Ctrl+Shift+X)输入
theme
搜索Dracula Official
点击安装
4、配置文件图标主题
- 打开扩展(Ctrl+Shift+X) 输入
theme
搜索Material Theme Icons
点击安装
二、安装C / C ++扩展。
打开扩展(Ctrl+Shift+X) 输入C / C ++
搜索C / C ++插件,作者为Microsoft
三、编译与调试配置
1、创建工作空间
启动Visual Studio Code,在界面下部终端处输入命令创建工作空间和项目目录
mkdir Clang_Workspace
cd Clang_Workspace
mkdir helloworld
cd helloworld
code .
code .
命令会打开一个新的Visual Studio Code工作窗体并导航到当前项目路径。在配置的过程中,Visual Studio Code会自动创建一个.vscode
文件夹,此文件夹中保存着一些自定义的项目配置文件。C语言开发主要用以下三个配置文件:c_cpp_properties.json
(编译器路径和IntelliSense设置)tasks.json
(编译设置)launch.json
(调试器设置)
2、创建配置文件
配置文件既可手动添加文件,也可以从命令面板中创建。
2.1 c_cpp_properties.json文件
打开命令面板(Ctrl+Shift+P)输入edit configurations
选择c/C++:Edit Configurations (JSON)
自动创建文件,可以自定义修改参数并保存即可。
{
"configurations": [
{
"name": "Win32",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
includePath
:包含目录路径或源文件目录路径compilerPath
:编译器的路径,开发工具使用它来推断C 标准库头文件的路径
2.2 tasks.json 文件
tasks.json
文件主要告诉VS Code如何构建(编译)程序。
打开命令面板(Ctrl+Shift+P)输入Configure Task
选择>Tasks:Configure Task
弹出使用模板创建tasks.json文件
。
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Microsoft_Task",
"command": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
label
:Tasks名称,在launch.json
使用此名称与tasks.json
作关联command
:定要运行的编译工具args
:该args
数组参数即是传送到command
中指定编译工具的命令行参数。其参数说明请另行查找相关说明
2.3 launch.json 文件
tasks.json
文件主要配置程序的调试信息
打开命令面板(Ctrl+Shift+P)输入Open launch.json
选择Debug:Open launch.json
找开文件
{
"version": "0.2.0",
"configurations": [
{
"name": "cl.exe build and debug active file",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false, //主函数入口是否添加调试断点
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "Microsoft_Task"
}
]
}