Home  >  Article  >  Development Tools  >  How to set up c language development environment with vscode

How to set up c language development environment with vscode

王林
王林Original
2020-02-13 14:43:368036browse

How to set up c language development environment with vscode

MinGw的下载与安装

因为windows下vscode不直接具备对于C语言的编译调试工具,所以要下载集成gcc等工具的MinGw以提供扩展支持。

How to set up c language development environment with vscode

任意选择一个(如果需要自定义安装gcc相关模块,也可以选择在线安装install.exe

将该文件夹解压到需要安装的目录

How to set up c language development environment with vscode

配置系统环境变量Path

打开我的电脑-属性-高级系统设置-高级-环境变量

在系统变量中找到Path变量,选择编辑(没有则新建),将之前mingw解压后目录下bin文件夹目录添加至其中,注意windows 7及以前版本需与原值用英文分号隔开,windows8及以上版本选择新建即可。

How to set up c language development environment with vscode

 或

How to set up c language development environment with vscode

然后在系统变量中找到include变量(没有则新建一个,不区分大小写),编辑,将原mingw解压目录下include文件夹目录添加进去。

How to set up c language development environment with vscode

修改vscode调试配置文件

重启vscode,打开菜单-调试-添加附加调试器...,找到左侧列表下的C/C++(找不到的话搜索试试)选择install安装后(可选操作,推荐)搜索code runner选择install安装

How to set up c language development environment with vscode

勾选下列两项

How to set up c language development environment with vscode

随意指定一个目录,新建文件夹,新建一个文件,将文件名修改为.c后缀的c代码文件,返回vscode,选择菜单-打开文件夹,选择刚才新建的文件夹,在左侧窗口单击刚新建的c代码文件,随意写一个简单的C程序

How to set up c language development environment with vscode

此时,单击窗口右上角的运行,即可在vscode下方终端中看到程序运行结果

How to set up c language development environment with vscode

How to set up c language development environment with vscode

配置C/C++ debuger

在之前新建的文件夹下再新建一个名为.vscode的文件夹,新建一个launch.json文件,以记事本或者vscode的方式打开,在里面输入:

{
        "version": "0.2.0",  
        "configurations": [  
            {  
                "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
                "type": "cppdbg",       // 配置类型,这里只能为cppdbg
                "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
                "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
                "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
                "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
                "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
                "environment": [],  
                "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
                "MIMode": "gdb",  
                "miDebuggerPath": "D:\\Install\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
                "preLaunchTask": "gcc", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
                "setupCommands": [  
                    {   
                "description": "Enable pretty-printing for gdb",  
                        "text": "-enable-pretty-printing",  
                        "ignoreFailures": true  
                    }  
                ]  
            }  
        ]  
    }

注意这里"miDebuggerPath": "D:\\Install\\bin\\gdb.exe"将其值改成自己所解压的mingw\bin\gdb.exe的路径

其他设置可看情况修改

在vscode中选择菜单-文件-打开文件夹,打开自己所新建的C代码下的文件夹,打开后选择左侧菜单对应的C代码文件,可选择菜单-调试-启动调试(快捷键F5)

弹出以下窗口

How to set up c language development environment with vscode

 选择第二个"配置任务"

How to set up c language development environment with vscode

在打开的task.json文件中输入:

{
    "version": "2.0.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"],    // 编译命令参数
    "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代码文件,菜单-调试-启动调试(快捷键F5),即可成功运行C程序!

相关推荐:vscode教程

The above is the detailed content of How to set up c language development environment with vscode. 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