search
HomeDevelopment ToolsVSCodevscode configures the environment for compiling and running c programs

vscode configures the environment for compiling and running c programs

Dec 16, 2019 am 09:36 AM
c programvscodecompilerunConfiguration

vscode configures the environment for compiling and running c programs

Background:

1. VS Code is just a code editor. Compiling and running these tasks require other programs to complete.

2. Selection of C/C compiler, GCC/G (MinFGW-w64)

3. MinGW and MinGW-w64 are two different projects. MinGW itself has not been updated for a long time, so it is not recommended. For convenience, MinGW in this article actually refers to MinGW-w64.

4. Use of command line and adding system variables.

Install vs code

vscode configures the environment for compiling and running c programs

Plug-in installation

1. Necessary plug-in

C/C (ms-vscode. cpptools, officially produced by Microsoft): The most complete C/C plug-in, and it is an official plug-in from Microsoft. You can download it with confidence

Code Runner (formulahendry.code-runner): quickly compile and run a single file, which is convenient.

vscode configures the environment for compiling and running c programs

2. Recommended plugin

Bracket Pair Colorizer: Rainbow brackets, matching brackets will be marked with the same color.

Material Icon Theme: Icon pack plug-in, "fancy", recommended.

One Dark Pro: theme plug-in, recommended Chinese Language Pack: Chinese language pack.

Configuration environment:

One-click configuration:

Valid for win7/10: If you don’t want to bother, download the one-click configuration compressed package. After decompression, open vscode_onesrc/ and find start Right click on .bat ->Run as administrator

vscode configures the environment for compiling and running c programs

The installation is successful and the display is as follows:

vscode configures the environment for compiling and running c programs

Manual configuration:

Configure the compiler:

Before running C/C with VS Code, you need to be able to compile C/C on the command line.

Here we need to use GCC to compile.

下载地址:https://sourceforge.net/projects/mingw-w64/

MinGW-W64 GCC-8.1.0

x86_64-win32-seh

i686-win32-dwarf

64位电脑选择x86_64,32位选择i686。

下载解压完成后,添加系统变量 当前路径\mingw64\bin 到path里面。设置好变量后,重启电脑。在命令行中输入gcc 或者g++

如果显示如下界面:

vscode configures the environment for compiling and running c programs

配置成功,进行下一步。

配置Json:

以下配置文件需要放在.vscode文件夹里面(注意有个点),如果工作目录为workSpace.需要将.vscode放在workSpace目录里面。

下面的C:\Users\15591\MyFile\Develop\mingw64\include(一共四个),需要换成你自己的gcc编译器安装路径,如果你的路径为C:mingw64\你可以换成C:mingw64include

c_cpp_properties.json

{
	"configurations": [{
		"name": "Win32",
		"defines": [
			"_DEBUG",
			"UNICODE",
			"_UNICODE"
		],
		"includePath": [
			"${workspaceFolder}",
			"C:\\Users\\15591\\MyFile\\Develop\\mingw64\\include"
		],
		"browse": {
			"path": [
				"${workspaceFolder}",
				"C:\\Users\\15591\\MyFile\\Develop\\mingw64\\include"
			],
			"limitSymbolsToIncludedHeaders": true,
			"databaseFilename": ""
		},
		"windowsSdkVersion": "10.0.17134.0",
		"compilerPath": "C:\\Users\\15591\\MyFile\\Develop\\mingw64\\bin\\gcc.exe",
		"cStandard": "c11",
		"cppStandard": "c++17"
	}],
	"version": 4
}

launch.json

{
	"version": "0.2.0",
	"configurations": [

		{
			"name": "(gdb) Launch", //配置名称;在启动配置下拉菜单中显示
			"type": "cppdbg",
			"request": "launch",
			"program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
			"args": [], //传入的参数
			"stopAtEntry": false,
			"cwd": "${workspaceFolder}",
			"environment": [],
			"externalConsole": true,
			"internalConsoleOptions": "neverOpen", 
			// 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
			"MIMode": "gdb",
			"miDebuggerPath": "C:\\Users\\15591\\MyFile\\Develop\\mingw64\\bin\\gdb.exe",
			"setupCommands": [{
				"description": "Enable pretty-printing for gdb",
				"text": "-enable-pretty-printing",
				"ignoreFailures": true
			}],
			"preLaunchTask": "CppCompile" 
			// 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
		}
	]
}

tasks.json

{
	"version": "2.0.0",
	"tasks": [{
			"label": "CppCompile", 
			// 任务名称,与launch.json的preLaunchTask相对应            
			"command": "g++", // 要使用的编译器,我们主要针对cpp文件调试,亦可以改成其他的            
			"args": [                
			"${file}",                
			"-o", // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out                
			"${fileDirname}/${fileBasenameNoExtension}.exe",                
			"-g", // 生成和调试有关的信息                
			"-Wall", // 开启额外警告                
			"-static-libgcc", // 静态链接                
			"-std=c++17" // C语言最新标准为c11,或根据自己的需要进行修改
		], // 编译命令参数            
		"type": "shell", // 可以为shell或process,前者相当于先打开shell再输入命令,后者是直接运行命令            
		"group": {                
		"kind": "build",                
		"isDefault": true // 设为false可做到一个tasks.json配置多个编译指令,需要自己修改本文件,我这里不多提
	},
	"presentation": {
		"echo": false,
		"reveal": "always", 
		// 在“终端”中显示编译信息的策略,可以为always,silent,never。具体参见VSC的文档                
		"focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译c和c++来说,设为true没有意义                
		"panel": "shared" // 不同的文件的编译信息共享一个终端面板
	},
	"problemMatcher": "$gcc"
}]
}

settings.json

{    
"workbench.colorTheme": "One Dark Pro",//主题One Dark Pro,如不需要删除本行    
"git.enabled": false,//关闭git    
"git.ignoreMissingGitWarning": true,//忽略git缺失警告    
"terminal.integrated.rendererType": "dom",    
"breadcrumbs.enabled": true,    
"workbench.iconTheme": "material-icon-theme",//图标主题,如不需要删除本行    
    "files.defaultLanguage": "cpp", // ctrl+N新建文件后默认的语言    
"editor.formatOnType": true, // 输入时就进行格式化,默认触发字符较少,分号可以触发    
"editor.snippetSuggestions": "top", // snippets代码优先显示补全    
"code-runner.runInTerminal": true, // 设置成false会在“输出”面板中输出,无法输入,建议设true    
"code-runner.executorMap": {        
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -std=c11 
&& $dir$fileNameWithoutExt",        
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe -Wall -g -Og -static-libgcc -std=c++17 
&& $dir$fileNameWithoutExt"
    },    
"code-runner.saveFileBeforeRun": true, // run code前保存    
"code-runner.preserveFocus": false, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false    
"code-runner.clearPreviousOutput": true, // 每次run code前清空属于code runner的终端消息    
"code-runner.ignoreSelection": true, 
}

HelloWorld:

file->open folder->vscode_onesrc

找到并打开我们的文件夹vscode_onesrc,打开HelloWorld.c点击右上角的三角形,编译运行!

Hello World!

vscode configures the environment for compiling and running c programs

vscode configures the environment for compiling and running c programs

vscode configures the environment for compiling and running c programs

相关文章教程推荐:vscode教程

The above is the detailed content of vscode configures the environment for compiling and running c programs. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Visual Studio Professional and Enterprise: Paid Versions and FeaturesVisual Studio Professional and Enterprise: Paid Versions and FeaturesMay 10, 2025 am 12:20 AM

The difference between VisualStudioProfessional and Enterprise is in the functionality and target user groups. The Professional version is suitable for professional developers and provides functions such as code analysis; the Enterprise version is for large teams and has added advanced tools such as test management.

Choosing Between Visual Studio and VS Code: The Right Tool for YouChoosing Between Visual Studio and VS Code: The Right Tool for YouMay 09, 2025 am 12:21 AM

VisualStudio is suitable for large projects, VSCode is suitable for projects of all sizes. 1. VisualStudio provides comprehensive IDE functions, supports multiple languages, integrated debugging and testing tools. 2.VSCode is a lightweight editor that supports multiple languages ​​through extension, has a simple interface and fast startup.

Visual Studio: A Powerful Tool for DevelopersVisual Studio: A Powerful Tool for DevelopersMay 08, 2025 am 12:19 AM

VisualStudio is a powerful IDE developed by Microsoft, supporting multiple programming languages ​​and platforms. Its core advantages include: 1. Intelligent code prompts and debugging functions, 2. Integrated development, debugging, testing and version control, 3. Extended functions through plug-ins, 4. Provide performance optimization and best practice tools to help developers improve efficiency and code quality.

Visual Studio vs. VS Code: Pricing, Licensing, and AvailabilityVisual Studio vs. VS Code: Pricing, Licensing, and AvailabilityMay 07, 2025 am 12:11 AM

The differences in pricing, licensing and availability of VisualStudio and VSCode are as follows: 1. Pricing: VSCode is completely free, while VisualStudio offers free community and paid enterprise versions. 2. License: VSCode uses a flexible MIT license, and the license of VisualStudio varies according to the version. 3. Usability: VSCode is supported across platforms, while VisualStudio performs best on Windows.

Visual Studio: From Code to ProductionVisual Studio: From Code to ProductionMay 06, 2025 am 12:10 AM

VisualStudio supports the entire process from code writing to production deployment. 1) Code writing: Provides intelligent code completion and reconstruction functions. 2) Debugging and testing: Integrate powerful debugging tools and unit testing framework. 3) Version control: seamlessly integrate with Git to simplify code management. 4) Deployment and Release: Supports multiple deployment options to simplify the application release process.

Visual Studio: A Look at the Licensing LandscapeVisual Studio: A Look at the Licensing LandscapeMay 05, 2025 am 12:17 AM

VisualStudio offers three license types: Community, Professional and Enterprise. The Community Edition is free, suitable for individual developers and small teams; the Professional Edition is annually subscribed, suitable for professional developers who need more functions; the Enterprise Edition is the highest price, suitable for large teams and enterprises. When selecting a license, project size, budget and teamwork needs should be considered.

The Ultimate Showdown: Visual Studio vs. VS CodeThe Ultimate Showdown: Visual Studio vs. VS CodeMay 04, 2025 am 12:01 AM

VisualStudio is suitable for large-scale project development, while VSCode is suitable for projects of all sizes. 1. VisualStudio provides comprehensive development tools, such as integrated debugger, version control and testing tools. 2.VSCode is known for its scalability, cross-platform and fast launch, and is suitable for fast editing and small project development.

Visual Studio vs. VS Code: Comparing the Two IDEsVisual Studio vs. VS Code: Comparing the Two IDEsMay 03, 2025 am 12:04 AM

VisualStudio is suitable for large projects and Windows development, while VSCode is suitable for cross-platform and small projects. 1. VisualStudio provides a full-featured IDE, supports .NET framework and powerful debugging tools. 2.VSCode is a lightweight editor that emphasizes flexibility and extensibility, and is suitable for various development scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.