search
HomeDevelopment ToolsVSCodeHow vscode compiles and runs c/c++ programs

How vscode compiles and runs c/c++ programs

Dec 12, 2019 pm 03:14 PM
cc++vscodeprogram

How vscode compiles and runs c/c++ programs

安装微软C/C++插件

在VS Code的扩展图标里搜索c++,安装C/C++插件并重载VS Code

How vscode compiles and runs c/c++ programs

重启VS Code之后,打开一个含c/c++源码的文件夹,VS Code将会创建一个名为.vscode的子文件夹用于存放配置文件。

安装MinGW

去官网下载安装器,安装好后选择需要安装的工具:

How vscode compiles and runs c/c++ programs

选择好之后应用更改:

How vscode compiles and runs c/c++ programs

然后修改系统的环境变量,将可执行文件目录加入到Path中:

How vscode compiles and runs c/c++ programs

智能提示

为实现代码补全,需要创建一个c_cpp_properties.json文件。新建一个hello.c,输入:

#include <stdio.h>
int main(void)
{
    printf("hello");
    getchar();
    return 0;
}

VS Code会提示找不到头文件,点击错误处的小灯泡,编辑包含路径

How vscode compiles and runs c/c++ programs

在c_cpp_properties.json中修改Win32下的路径设置

"browse": {
    "path": [
        "${workspaceRoot}",
        "C:\\DevProgram\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++"
    ],
    "limitSymbolsToIncludedHeaders": true,
    "databaseFilename": ""
}

返回编辑器,点击小灯泡,将头文件所在目录添加到包含路径中

How vscode compiles and runs c/c++ programs

构建程序

构建程序需要创建一个tasks.json文件,点击任务->配置任务:

How vscode compiles and runs c/c++ programs

在弹出的选项中选择Others,然后修改tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build hello",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "hello.c"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

调试代码

调试代码需要创建一个launch.json文件,点击调试栏的齿轮图标

How vscode compiles and runs c/c++ programs

修改launch.json

{
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(gdb) Launch",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/a.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": true,
                "MIMode": "gdb",
                "miDebuggerPath": "C:\\DevProgram\\MinGW\\bin\\gdb.exe",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "build hello"
            }
        ]
    }

调试运行

返回编辑器,设置断点,按F5开始调试

How vscode compiles and runs c/c++ programs

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

The above is the detailed content of How vscode compiles and runs c/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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools