VsCode如何配置C语言环境?

打印 上一主题 下一主题

主题 853|帖子 853|积分 2561

以前学校上C语言课程都是使用VC++6.0上课,这个学期为了复习一遍C语言,而且自己经常使用VSCode开发前端项目,所以为了方便。这里把需要的环境和插件配置在这里介绍一下。
Windows环境下安装C语言编译器

MinGW-W64 GCCC的下载地址为:https://sourceforge.net/projects/mingw-w64/
下载完之后进行环境配置
  1. 我的电脑->属性->高级系统设置->环境变量->系统变量->path->新建一个路径(比如:D:\ProgramFiles\gcc\mingw64\bin)
复制代码
在随后出现的”cmd.exe"窗口中,输入“gcc -v"命令,会出现gcc的版本号,说明安装配置成功。
下载VsCode插件


  • Code Runner
  • C/C++
在随后的Settings页面中,将“Run Code Configuration”中的“Run In Terminal"、"Save File Before Run"前面的复选框选中。

配置插件环境

.vscode下的文件:
  1. launch.json
  2. {
  3.     // 使用 IntelliSense 了解相关属性。
  4.     // 悬停以查看现有属性的描述。
  5.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  6.     "version": "0.2.0",
  7.     "configurations": [
  8.         {
  9.             "name": "(gdb) Windows 上的 Bash 启动",
  10.             "type": "cppdbg",
  11.             "request": "launch",
  12.             "program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
  13.             "args": [],
  14.             "stopAtEntry": false,
  15.             "cwd": "/mnt/${fileDirname}",
  16.             "environment": [],
  17.             "externalConsole": false,
  18.             "pipeTransport": {
  19.                 "debuggerPath": "/usr/bin/gdb",
  20.                 "pipeProgram": "${env:windir}\\system32\\bash.exe",
  21.                 "pipeArgs": ["-c"],
  22.                 "pipeCwd": ""
  23.             },
  24.             "setupCommands": [
  25.                 {
  26.                     "description": "为 gdb 启用整齐打印",
  27.                     "text": "-enable-pretty-printing",
  28.                     "ignoreFailures": true
  29.                 },
  30.                 {
  31.                     "description": "将反汇编风格设置为 Intel",
  32.                     "text": "-gdb-set disassembly-flavor intel",
  33.                     "ignoreFailures": true
  34.                 }
  35.             ]
  36.         }
  37.     ]
  38. }
复制代码
  1. settings.json
  2. {
  3.     "C_Cpp.errorSquiggles": "disabled",
  4.     "files.associations": {
  5.         "windows.h": "c",
  6.         "stdio.h": "c",
  7.         "stdlib.h": "c"
  8.     }
  9. }
复制代码
  1. tasks.json
  2. {
  3.     "tasks": [
  4.         {
  5.             "type": "cppbuild",
  6.             "label": "C/C++: gcc.exe 生成活动文件",
  7.             "command": "D:\\ProgramFiles\\gcc\\mingw64\\bin\\gcc.exe",
  8.             "args": [
  9.                 "-fdiagnostics-color=always",
  10.                 "-g",
  11.                 "${file}",
  12.                 "-o",
  13.                 "${fileDirname}\\${fileBasenameNoExtension}.exe"
  14.             ],
  15.             "options": {
  16.                 "cwd": "${fileDirname}"
  17.             },
  18.             "problemMatcher": [
  19.                 "$gcc"
  20.             ],
  21.             "group": {
  22.                 "kind": "build",
  23.                 "isDefault": true
  24.             },
  25.             "detail": "调试器生成的任务。"
  26.         }
  27.     ],
  28.     "version": "2.0.0"
  29. }
复制代码
运行

我们编写一个C语言程序:
  1. /**
  2. * 需求:
  3. *  (1)、将整数 5 的第 2 个二进制置1        解法:将0101 | 0010 => 0111(最终结果)
  4.     (2)、将整数 7 的第 4 个二进制位取反      解法:已经0111(7),只要 ^(异或)上1000就可以得到:1111
  5.     (3)、将整数 15 右移 2 位,再将第 2 个二进制位置 0  解法:先将1111(15)>>2,然后0011 & 1101 => 0001
  6.     (4)、设变量 a 的二进制数是 10101101,若想通过运算 a^b 使得 a的中间 4 位取反,其余位不变,则 b 的值是多少 ?
  7.        1    1    1    1    1    1    1    1
  8.        128  64   32   16   8    4    2    1
  9.        解法:
  10.        10101101
  11.        128+32+8+4+1=173
  12.        ----
  13.        00111100
  14.        32+16+8+4=60
  15.        ----
  16.        10010001
  17.        128+16+1=145
  18.        ----
  19.        注意位置:1010 第一个位置是0,第二个位置是1。
  20. */
  21. #include <windows.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. int main(void)
  25. {
  26.     SetConsoleOutputCP(65001);
  27.     int num1 = 5;
  28.     int result1 = num1 | 2;
  29.     printf("%d\n",result1);
  30.     int num2 = result1;
  31.     int result2 = num2 ^ 8;
  32.     printf("%d\n",result2);
  33.     int num3 = result2;
  34.     int result3 = (num3 >> 2) & 13;
  35.     printf("%d\n",result3);
  36.     int num4 = 173;
  37.     int result4 = num4 ^ 60;
  38.     printf("%d\n",result4);
  39.     getchar();
  40.     return 0;
  41. }
复制代码
然后对文件右键点击Run Code就可以生成exe文件并运行了

调试

这个问题暂时未解决,等待未来的自己解决。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

祗疼妳一个

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表