温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在vscode编写c语言的方法

发布时间:2020-12-10 11:49:47 来源:亿速云 阅读:417 作者:小新 栏目:软件技术

这篇文章将为大家详细讲解有关在vscode编写c语言的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

1、安装vscode(版本1.27)

https://code.visualstudio.com/ 下载安装vscode。

2、安装c/c++扩展。

在vscode编写c语言的方法

3、安装编译工具mingw-w64,http://www.mingw-w64.org/doku.php/download

配置环境变量,以WIN10为例 ,此电脑-属性-高级系统设置-环境变量-系统变量-path-添加一条D:\Program Files\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin(你安装编译工具路径)

配置前

在vscode编写c语言的方法

配置后

在vscode编写c语言的方法

-ps:如果开着vscode配置环境变量,配置完要关掉vscode重开一次。

4、配置文件launch.json,task.json。

新建文件hello.cpp,

在vscode编写c语言的方法

按F5弹出选择环境,配置launch.json

在vscode编写c语言的方法

点击进去,configurations:里内容如下:

{ 
"name": "(gdb) Launch", 
"type": "cppdbg",
"request": "launch", 
"program": "enter program name, for example ${workspaceFolder}/a.exe", 
"args": [], 
"stopAtEntry": false, 
"cwd": "${workspaceFolder}", 
"environment": [], 
"externalConsole": true, 
"MIMode": "gdb", 
"miDebuggerPath": "/path/to/gdb", 
"setupCommands": [ 
{ 
"description": "Enable pretty-printing for gdb", 
"text": "-enable-pretty-printing", 
"ignoreFailures": true 
} 
] 
}

修改成如下:

{ 
"name": "(gdb) Launch", 
"type": "cppdbg",
"request": "launch", 
"program": "${workspaceFolder}/a.exe",//这里删除前面那里的enter program name, for example 
"args": [], 
"stopAtEntry": false, 
"cwd": "${workspaceFolder}", 
"environment": [], 
"externalConsole": true, 
"MIMode": "gdb", 
"miDebuggerPath": "D:\\Program Files\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
//修改为你安装mingw32的路径 
"setupCommands": [ 
{ 
"description": "Enable pretty-printing for gdb", 
"text": "-enable-pretty-printing", 
"ignoreFailures": true 
} 
], 
"preLaunchTask": "build hello",//task.json里面的名字 
}

回到hello.cpp按F5弹出报错框,选配置任务。

在vscode编写c语言的方法

点击,然后选Others,出现task.json,如下:

"version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]

修改如下:

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

注意:如果是win32程序(窗口界面),args内加上"-mwindows"。

在vscode编写c语言的方法

回到hello.cpp文件,按F5。成功运行编译。

在vscode编写c语言的方法

5、还有就是发现中文控制台显示乱码。

在vscode编写c语言的方法

只要点击右下角utf-8。

在vscode编写c语言的方法

点击使用编码保存,选GBK,然后按F5运行。

在vscode编写c语言的方法

关于在vscode编写c语言的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI