如何在vscode编写c语言


本文摘自PHP中文网,作者爱喝马黛茶的安东尼,侵删。

1、安装vscode(版本1.27)

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

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

57c0c32f77d2acc2d72443888eefb56.png

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(你安装编译工具路径)

配置前

ed4f4b963f7554cce1be48ad21bd213.png

配置后

40b2ad2f449bec2baa8234f30165bef.png

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

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

新建文件hello.cpp,

dfba4c6d9f5c4601f94192b1371926f.png

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

496fc3a6e8c8f773d2fcfa5282df9fc.png

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

{

"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

}

]

}

修改成如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

{

"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弹出报错框,选配置任务。

8e075ae4422a2d1ab29ee8bd2fa3a5f.png

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

1

2

3

4

5

6

7

8

"version": "2.0.0",

    "tasks": [

        {

            "label": "echo",

            "type": "shell",

            "command": "echo Hello"

        }

    ]

修改如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

"version": "2.0.0",

    "tasks": [

        {

            "label": "build hello",

            "type": "shell",

            "command": "g++",

            "args": [

                "-g", "hello.cpp",

            ],

            "group":{

                "kind": "build",

                "isDefault": true

            }

        }

    ]

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

ec64620c9eedbbb94daa040721eb63a.png

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

1575604263361594.png

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

aeaead2bfb88f8271ff083645d60162.png

只要点击右下角utf-8。

92c6f0e94a648324a9dc3d5355d6c07.png

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

ea14d4d596b1163202c386878de42a0.png

PHP中文网,有大量免费vscode入门教程,欢迎大家学习!

以上就是如何在vscode编写c语言的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

如何在vscode编写c语言

更多相关阅读请进入《如何在vscode编写c语言》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...