添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

安装GCC

先安装C/C++编译器,如果已经安装好了,这几步可以跳过。

  • 打开 WinLibs 。个人建议一般情况下选择UCRT runtime,但如果需要用 EasyX ,就选择MSVCRT runtime(我更建议去用 VS )。可以选最新版本,目前是GCC 13.2.0;也可以选自己需要的其他版本。在POSIX和MCF之间,个人建议选择POSIX。如果在用64位机,就选择Win64的 Zip archive ,若安装了 7-Zip 等压缩软件也可以下载 7-Zip archive ,能比zip小不少;如果在用32位机,就相应地选择Win32的 Zip archive 7-Zip archive 。这里以下载 UCRT runtime GCC 13.2.0 (with POSIX threads) Win64 Zip archive 为例。 友情提示 :压缩包从 GitHub 下载,如果无法访问GitHub,请看 这里
  • 右键点击压缩包,点击“全部解压缩”,选择要将GCC解压到的目标位置。这里以解压到 C:\gcc-13.2.0 为例。 如果除了C盘还有其他分区(比如D盘),也可以不放在C盘。除了路径不一样以外,步骤都一样。
  • 按Win+R键打开“运行”(Run)窗口,在里面输入 control ,再按下回车(Enter)键,打开控制面板。
  • 在控制面板右上角搜索栏输入“env”,此时只能搜索到1条设置项。点击“编辑系统环境变量”。
  • 在弹出的“系统属性”对话框上(下图左侧)点击“环境变量”按钮,弹出“环境变量”对话框(下图中部)。在下半部分的“系统变量”中找到Path。
  • 双击上述对话框“系统变量”的Path一行,打开“编辑环境变量”对话框,点击“新建”按钮。
  • 打开GCC解压到的文件夹,这里是 C:\gcc-13.2.0 ,换成你解压到的文件夹。进入 mingw64 ,再进入 bin ,可以看到里面有很多exe文件(可执行文件)。把这个 bin 文件夹的完整路径 C:\gcc-13.2.0\mingw64\bin 复制下来,粘贴到系统环境变量Path原有内容的后面。如果是Win32版,这个路径就是 C:\gcc-13.2.0\mingw32\bin
  • 类似地进入 mingw64\x86_64-w64-mingw32\bin ,这里面也有一些exe文件。把这个 bin 文件夹的完整路径 C:\gcc-13.2.0\mingw64\x86_64-w64-mingw32\bin 也复制下来,粘贴到系统环境变量Path原有内容的后面。如果是Win32版,这个路径就是 C:\gcc-13.2.0\mingw32\i686-w64-mingw32\bin
  • 点击“确定”,关掉设置环境变量弹出的对话框。
  • 按Win+R键打开“运行”窗口,输入 cmd ,打开命令提示符(Command Prompt),在里面输入 gcc -v 。如果输出“gcc不是内部或外部命令,也不是可运行的程序或批处理文件”,重启一下电脑试试;如果还是这样,就检查一下之前操作哪块有问题,或者重做一遍。如果输出类似下图,再测试一下 g++ -v gdb -v ,也都是这样的版本信息,则GCC已安装完毕。
  • 配置C语言环境

    1. 打开VS Code,按Ctrl+Shift+X键,搜索“c/c++”插件并点击“Install”安装。
    2. 按Ctrl+K Ctrl+O“打开文件夹”(Open Folder),选择或新建一个文件夹,作为写C语言程序的工作区(workspace)。这里以 C:\Users\vboxuser\Documents\c 为例。
    3. 在工作区新建一个子文件夹,命名为 .vscode
    4. .vscode 文件夹里新建一个文件,命名为 launch.json
    5. 把如下代码复制到 launch.json
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      {
      "configurations": [
      {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "gdb",
      "setupCommands": [
      {
      "description": "Enable pretty-printing for gdb",
      "text": "-enable-pretty-printing",
      "ignoreFailures": true,
      },
      {
      "description": "Set Disassembly Flavor to Intel",
      "text": "-gdb-set disassembly-flavor intel",
      "ignoreFailures": true,
      },
      ],
      "preLaunchTask": "C/C++: gcc build active file",
      },
      ],
      }
      然后按Ctrl+S保存。
    6. 和刚才一样,再在 .vscode 文件夹里新建一个 tasks.json 文件,写入如下代码
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      {
      "tasks": [
      {
      "type": "cppbuild",
      "label": "C/C++: gcc build active file",
      "command": "gcc",
      "args": [
      "-fdiagnostics-color=always",
      "-Wall",
      "-Wextra",
      "-Wpedantic",
      "-Wshadow",
      "-g",
      "${file}",
      "-o",
      "${fileDirname}\\${fileBasenameNoExtension}.exe",
      ],
      "options": {
      "cwd": "${fileDirname}",
      },
      "problemMatcher": [
      "$gcc",
      ],
      "group": {
      "kind": "build",
      "isDefault": true,
      },
      "detail": "Task generated by Debugger.",
      },
      ],
      "version": "2.0.0",
      }
      然后按Ctrl+S保存。
    7. 打开该工作区后,在工作区或其子文件夹里新建一个文件 hello.c ,再在里面写点东西,比如
      1
      2
      3
      4
      5
      6
      #include <stdio.h>

      int main() {
      printf("hello, world\n");
      return 0;
      }
      然后按Ctrl+S保存。 不要把源代码文件放在 .vscode
    8. 按F5(或Fn+F5,下同)调试程序,在下方调试控制台(Debug Console)看到程序正常结束,输出(黄字)以 exited with code 0 (0x00000000) 结尾。
    9. 点击下方“终端”(Terminal),看到输出 hello, world ,则环境配置成功。
    10. 配置C++环境

      1. 打开VS Code,按Ctrl+Shift+X键,搜索“c/c++”插件并点击“Install”安装。
      2. 按Ctrl+K Ctrl+O“打开文件夹”(Open Folder),选择或新建一个 本来不是工作区的 文件夹,作为写C++程序的工作区。
      3. 在工作区新建一个子文件夹,命名为 .vscode
      4. .vscode 文件夹里新建一个 launch.json 文件,写入如下代码
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        {
        "configurations": [
        {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "gdb",
        "setupCommands": [
        {
        "description": "Enable pretty-printing for gdb",
        "text": "-enable-pretty-printing",
        "ignoreFailures": true,
        },
        {
        "description": "Set Disassembly Flavor to Intel",
        "text": "-gdb-set disassembly-flavor intel",
        "ignoreFailures": true,
        },
        ],
        "preLaunchTask": "C/C++: gcc build active file",
        },
        ],
        }
        然后按Ctrl+S保存。
      5. 和刚才一样,再在 .vscode 文件夹里新建一个 tasks.json 文件,写入如下代码
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        {
        "tasks": [
        {
        "type": "cppbuild",
        "label": "C/C++: gcc build active file",
        "command": "g++",
        "args": [
        "-fdiagnostics-color=always",
        "-Wall",
        "-Wextra",
        "-Wpedantic",
        "-Wshadow",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        ],
        "options": {
        "cwd": "${fileDirname}",
        },
        "problemMatcher": [
        "$gcc",
        ],
        "group": {
        "kind": "build",
        "isDefault": true,
        },
        "detail": "Task generated by Debugger.",
        },
        ],
        "version": "2.0.0",
        }
        然后按Ctrl+S保存。
      6. 打开该工作区后,在工作区或其子文件夹里新建一个文件 hello.cpp ,再在里面写点东西,比如
        1
        2
        3
        4
        5
        6
        #include <iostream>

        int main() {
        std::cout << "hello, world\n";
        return 0;
        }
        然后按Ctrl+S保存。同样 不要把源代码文件放在 .vscode
      7. 按F5调试程序,在下方调试控制台(Debug Console)看到程序正常结束,输出(黄字)以 exited with code 0 (0x00000000) 结尾。
      8. 点击下方“终端”(Terminal),看到输出 hello, world ,则环境配置成功。
      9. Linux

        绝大多数Linux发行版都内置gcc,但可能没有g++和gdb。

        安装g++、gdb

        Debian系( Debian Kali Mint Ubuntu 等),在Shell中执行

        1
        sudo apt install g++ gdb

        Red Hat系默认用dnf的发行版( CentOS 8、 Fedora 22+、 RHEL 8+ 等),在Shell中执行

        1
        sudo dnf install gcc-c++ gdb

        Red Hat系默认用yum的发行版( CentOS 7-、 Fedora 21-、 RHEL 7- 等),在Shell中执行

        1
        sudo yum install gcc-c++ gdb

        Red Hat系的 openSUSE 默认用zypper,在Shell中执行

        1
        sudo zypper install gcc-c++ gdb

        Arch系( Arch Linux Manjaro 等),在Shell中执行

        1
        sudo pacman -S gdb

        配置C语言环境

        1. 打开VS Code,按Ctrl+Shift+X键,搜索“c/c++”插件并点击“Install”安装。
        2. 按Ctrl+K Ctrl+O“打开文件夹”(Open Folder),选择或新建一个文件夹,作为写C语言程序的工作区(workspace)。这里以 ~/Documents/c 为例。
        3. 在工作区新建一个子文件夹,命名为 .vscode
        4. .vscode 文件夹里新建一个文件,命名为 launch.json ,写入如下代码
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          {
          "configurations": [
          {
          "name": "(gdb) Launch",
          "type": "cppdbg",
          "request": "launch",
          "program": "${fileDirname}/${fileBasenameNoExtension}",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${fileDirname}",
          "environment": [],
          "externalConsole": false,
          "MIMode": "gdb",
          "miDebuggerPath": "gdb",
          "miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; gdb -q --interpreter=mi",
          "setupCommands": [
          {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true,
          },
          {
          "description": "Set Disassembly Flavor to Intel",
          "text": "-gdb-set disassembly-flavor intel",
          "ignoreFailures": true,
          },
          ],
          "preLaunchTask": "C/C++: gcc build active file",
          },
          ],
          }
          然后按Ctrl+S保存。
        5. 和刚才一样,再在 .vscode 文件夹里新建一个 tasks.json 文件,写入如下代码
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          {
          "tasks": [
          {
          "type": "cppbuild",
          "label": "C/C++: gcc build active file",
          "command": "gcc",
          "args": [
          "-fdiagnostics-color=always",
          "-Wall",
          "-Wextra",
          "-Wpedantic",
          "-Wshadow",
          "-g",
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}",
          ],
          "options": {
          "cwd": "${fileDirname}",
          },
          "problemMatcher": [
          "$gcc",
          ],
          "group": {
          "kind": "build",
          "isDefault": true,
          },
          "detail": "Task generated by Debugger.",
          },
          ],
          "version": "2.0.0",
          }
          然后按Ctrl+S保存。
        6. 打开该工作区后,在工作区或其子文件夹里新建一个文件 hello.c ,再在里面写点东西,比如
          1
          2
          3
          4
          5
          6
          #include <stdio.h>

          int main() {
          printf("hello, world\n");
          return 0;
          }
          然后按Ctrl+S保存。 不要把源代码文件放在 .vscode
        7. 按F5调试程序,在下方调试控制台(Debug Console)看到程序正常结束,输出(黄字)以 exited with code 0 (0x00000000) 结尾。
        8. 点击下方“终端”(Terminal),看到输出 hello, world ,则环境配置成功。
        9. 配置C++环境

          1. 打开VS Code,按Ctrl+Shift+X键,搜索“c/c++”插件并点击“Install”安装。
          2. 按Ctrl+K Ctrl+O“打开文件夹”(Open Folder),选择或新建一个 本来不是工作区的 文件夹,作为写C++程序的工作区。
          3. 在工作区新建一个子文件夹,命名为 .vscode
          4. .vscode 文件夹里新建一个 launch.json 文件,写入如下代码
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            {
            "configurations": [
            {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "gdb",
            "miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; gdb -q --interpreter=mi",
            "setupCommands": [
            {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true,
            },
            {
            "description": "Set Disassembly Flavor to Intel",
            "text": "-gdb-set disassembly-flavor intel",
            "ignoreFailures": true,
            },
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            },
            ],
            }
            然后按Ctrl+S保存。
          5. 和刚才一样,再在 .vscode 文件夹里新建一个 tasks.json 文件,写入如下代码
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            {
            "tasks": [
            {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "g++",
            "args": [
            "-fdiagnostics-color=always",
            "-Wall",
            "-Wextra",
            "-Wpedantic",
            "-Wshadow",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}",
            ],
            "options": {
            "cwd": "${fileDirname}",
            },
            "problemMatcher": [
            "$gcc",
            ],
            "group": {
            "kind": "build",
            "isDefault": true,
            },
            "detail": "Task generated by Debugger.",
            },
            ],
            "version": "2.0.0",
            }
            然后按Ctrl+S保存。
          6. 打开该工作区后,在工作区或其子文件夹里新建一个文件 hello.cpp ,再在里面写点东西,比如
            1
            2
            3
            4
            5
            6
            #include <iostream>

            int main() {
            std::cout << "hello, world\n";
            return 0;
            }
            然后按Ctrl+S保存。同样 不要把源代码文件放在 .vscode
          7. 按F5调试程序,在下方调试控制台(Debug Console)看到程序正常结束,输出(黄字)以 exited with code 0 (0x00000000) 结尾。
          8. 点击下方“终端”(Terminal),看到输出 hello, world ,则环境配置成功。
          9. macOS

            安装开发者工具

            先安装含有C/C++编译器的开发者工具(Developer Tools),如果已经安装好了,这几步可以跳过。

          10. 打开“终端”,执行
            1
            clang --version
            如果已经安装好,这时应该输出版本信息;如果还没安装,这时会出现对话框,询问是否要安装开发者工具。选择“安装”。
          11. 安装过程中会出现 许可协议 阅读 并选择“同意”。如果不同意,可以停止安装,换一款工具。
          12. 安装好以后,在“终端”分别执行
            1
            2
            3
            clang --version
            clang++ --version
            lldb --version
            如果都能正常输出下图这样的版本信息,则开发者工具已安装完毕。
          13. 配置C语言环境

            1. 打开VS Code,按Cmd+Shift+X键,搜索“c/c++”插件并点击“Install”安装。
            2. 按Cmd+O,选择或新建一个文件夹,作为写C语言程序的工作区(workspace)。这里以 ~/Documents/c 为例。
            3. 在工作区新建一个子文件夹,命名为 .vscode
            4. .vscode 文件夹里新建一个文件,命名为 launch.json
            5. 把如下代码复制到 launch.json
              1
              2
              3
              4
              5
              6
              7
              8
              9
              10
              11
              12
              13
              14
              15
              16
              17
              {
              "configurations": [
              {
              "name": "(lldb) Launch",
              "type": "cppdbg",
              "request": "launch",
              "program": "${fileDirname}/${fileBasenameNoExtension}.out",
              "args": [],
              "stopAtEntry": false,
              "cwd": "${fileDirname}",
              "environment": [],
              "externalConsole": false,
              "MIMode": "lldb",
              "preLaunchTask": "C/C++: clang build active file",
              },
              ],
              }
              然后按Cmd+S保存。
            6. 和刚才一样,再在 .vscode 文件夹里新建一个 tasks.json 文件,写入如下代码
              1
              2
              3
              4
              5
              6
              7
              8
              9
              10
              11
              12
              13
              14
              15
              16
              17
              18
              19
              20
              21
              22
              23
              24
              25
              26
              27
              28
              29
              {
              "tasks": [
              {
              "type": "cppbuild",
              "label": "C/C++: clang build active file",
              "command": "/usr/bin/clang",
              "args": [
              "-fcolor-diagnostics",
              "-fansi-escape-codes",
              "-g",
              "${file}",
              "-o",
              "${fileDirname}/${fileBasenameNoExtension}.out",
              ],
              "options": {
              "cwd": "${fileDirname}",
              },
              "problemMatcher": [
              "$gcc",
              ],
              "group": {
              "kind": "build",
              "isDefault": true,
              },
              "detail": "Task generated by Debugger.",
              },
              ],
              "version": "2.0.0",
              }
              然后按Cmd+S保存。
            7. 打开该工作区后,在工作区或其子文件夹里新建一个文件 hello.c ,再在里面写点东西,比如
              1
              2
              3
              4
              5
              6
              #include <stdio.h>

              int main() {
              printf("hello, world\n");
              return 0;
              }
              然后按Cmd+S保存。 不要把源代码文件放在 .vscode
            8. 按F5调试程序,运行过程中可能会有对话框,提示编译出来的程序 hello.out 想要访问某个文件夹内的文件,选择“好”(除非你对自己写的程序很没有把握)。
            9. 在下方调试控制台(Debug Console)看到程序正常结束,输出蓝字 hello, world ,黄字以 exited with code 0 (0x00000000) 结尾。
            10. 配置C++环境

              1. 打开VS Code,按Cmd+Shift+X键,搜索“c/c++”插件并点击“Install”安装。
              2. 按Cmd+O选择或新建一个 本来不是工作区的 文件夹,作为写C++程序的工作区。
              3. 在工作区新建一个子文件夹,命名为 .vscode
              4. .vscode 文件夹里新建一个 launch.json 文件,写入如下代码
                1
                2
                3
                4
                5
                6
                7
                8
                9
                10
                11
                12
                13
                14
                15
                16
                17
                {
                "configurations": [
                {
                "name": "(lldb) Launch",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}/${fileBasenameNoExtension}.out",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "lldb",
                "preLaunchTask": "C/C++: clang++ build active file",
                },
                ],
                }
                然后按Cmd+S保存。
              5. 和刚才一样,再在 .vscode 文件夹里新建一个 tasks.json 文件,写入如下代码
                1
                2
                3
                4
                5
                6
                7
                8
                9
                10
                11
                12
                13
                14
                15
                16
                17
                18
                19
                20
                21
                22
                23
                24
                25
                26
                27
                28
                29
                {
                "tasks": [
                {
                "type": "cppbuild",
                "label": "C/C++: clang++ build active file",
                "command": "/usr/bin/clang++",
                "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out",
                ],
                "options": {
                "cwd": "${fileDirname}",
                },
                "problemMatcher": [
                "$gcc",
                ],
                "group": {
                "kind": "build",
                "isDefault": true,
                },
                "detail": "Task generated by Debugger.",
                },
                ],
                "version": "2.0.0",
                }
                然后按Cmd+S保存。
              6. 打开该工作区后,在工作区或其子文件夹里新建一个文件 hello.cpp ,再在里面写点东西,比如
                1
                2
                3
                4
                5
                6
                #include <iostream>

                int main() {
                std::cout << "hello, world\n";
                return 0;
                }
                然后按Cmd+S保存。同样 不要把源代码文件放在 .vscode
              7. 按F5调试程序,运行过程中可能会有对话框,提示编译出来的程序 hello.out 想要访问某个文件夹内的文件,选择“好”(除非你对自己写的程序很没有把握)。
              8. 在下方调试控制台(Debug Console)看到程序正常结束,输出蓝字 hello, world ,黄字以 exited with code 0 (0x00000000) 结尾,则环境配置成功。
              9. 如果想在外部控制台(窗口)显示输出,可以把上面 launch.json 第12行的 "externalConsole": false 改为 "externalConsole": true 。如果控制台闪退,可以在最后加 getchar(); system("pause"); 等语句,不让程序直接退出。
              10. 有关路径和文件名最好没有非ASCII字符(比如中文)和空格。
              11. console debug download extension folder install launch release terminal version workspace