美文网首页
搭建C环境

搭建C环境

作者: 代瑶 | 来源:发表于2021-11-26 16:33 被阅读0次

    编辑器用VSCode
    https://code.visualstudio.com/docs/?dv=win

    下载文件,
    https://sourceforge.net/projects/mingw-w64/files/

    image.png
    也可以用我的云盘文件
    链接:https://pan.baidu.com/s/1bvqd7W-cQBOy7tCCZEfkvQ 
    提取码:tp2p 
    --来自百度网盘超级会员V1的分享
    

    解压出来后将bin文件对应目录配置到环境变量中
    PATH下配置 bin路径


    image.png

    新建系统环境变量


    image.png

    gcc -v查看当前计算机mingw是否安装成功

    插件下载


    1637914543(1).png image.png image.png

    codeRunner 插件点击右键,需要设置Run In Terminal

    完成后就可以运行了, 贴一个简单的代码

    #include <stdio.h>
    
    int sum(int x, int y)
    {
       return x + y;
    }
    
    int main()
    {
       int total = sum(1,32);
       printf("%d",total);
       // printf("Hello, World!");
       return 0;
    }
    
    

    如果要调试怎么办呢
    用vscode打开一个文件夹, 然后找到.vscode 文件夹下面的launch.json进行修改

    一定要注意miDebuggerPath对应的值更改为自己的gdb路径

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Run C/C++",
          "type": "cppdbg",
          "request": "launch",
          "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": true,
          "MIMode": "gdb",
          "miDebuggerPath": "D:/important/mingw64/bin/gdb.exe",
          "setupCommands": [
            {
              "description": "Enable pretty-printing for gdb",
              "text": "-enable-pretty-printing",
              "ignoreFailures": false
            }
          ],
          "preLaunchTask": "build & run file"
        },
        {
          "name": "Debug C/C++",
          "type": "cppdbg",
          "request": "launch",
          "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": true,
          "MIMode": "gdb",
          "miDebuggerPath": "D:/important/mingw64/bin/gdb.exe",
          "setupCommands": [
            {
              "description": "Enable pretty-printing for gdb",
              "text": "-enable-pretty-printing",
              "ignoreFailures": false
            }
          ],
          "preLaunchTask": "build & debug file"
        },
        {
          "name": "C/C++ Runner: Debug Session",
          "type": "cppdbg",
          "request": "launch",
          "args": [],
          "stopAtEntry": false,
          "cwd": "e:/vscode/project/test",
          "environment": [],
          "program": "e:/vscode/project/test/build/Debug/outDebug",
          "internalConsoleOptions": "openOnSessionStart",
          "MIMode": "gdb",
          "miDebuggerPath": "D:/important/mingw64/bin/gdb.exe",
          "externalConsole": false,
          "setupCommands": [
            {
              "description": "Enable pretty-printing for gdb",
              "text": "-enable-pretty-printing",
              "ignoreFailures": true
            }
          ]
        }
      ]
    }
    

    新建文件tasks.json

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build & debug file",
                "type": "shell",
                "command": "g++",
                "args": [
                    "-g",
                    "-o",
                    "${fileBasenameNoExtension}",
                    "${file}"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            },
            {
                "label": "build & run file",
                "type": "shell",
                "command": "g++",
                "args": [
                    "-o",
                    "${fileBasenameNoExtension}",
                    "${file}"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

    在vs里面打个断点然后运行吧!


    image.png

    说一下中文乱码问题

    VSCode 输出是中文的, 是因为cmd 默认使用gbk2312
    打开命令窗口

    PS C:\Users\Admin> chcp
    活动代码页: 936
    

    在vscode 中打开设置, 到setting.json

    
        "terminal.integrated.profiles.windows": {
            "PowerShell": {
            "source": "PowerShell",
            "overrideName": true,
            "args": ["-NoExit", "/c", "chcp 65001"],
            "icon": "terminal-powershell", 
            }
        },
        "terminal.integrated.defaultProfile.windows": "PowerShell", 
    

    方法2:

    image.png
    image.png

    保存退出

    相关文章

      网友评论

          本文标题:搭建C环境

          本文链接:https://www.haomeiwen.com/subject/ubhxxrtx.html