预准备

1 安装VSCode和相关扩展

VSCode官网:Visual Studio Code - Code Editing. Redefined

C/C++扩展:

  • 这个扩展仅提供C/C++的语言支持,即编辑提示和debugging界面。
  • 不包括C/C++编译器(compiler和debugger)
VSCode 扩展 C/C++

2 安装C/C++编译器

选择MinGW(Minimalist GNU on Windows)

  • MinGW是GCC编译器的Windows版本,包含Win32API,可以将源代码编译为Windows系统可执行文件。
  • 一般选择MinGW-w64,可以用于编译64位和32位的可执行程序。

安装:

版本信息选择:

  • Version:GCC版本。一般选最高的就行(例如8.1.0)。
  • Architecture:电脑系统架构。64位选x86_64,32位选i686。
  • Threads:操作系统接口协议。Windows选择win32,Linux/Unix、MacOS选择posix。
  • Exception:异常处理模型。一般选择seh(新的模型,不支持32位),sjlj稳定性好(支持32位)。

这里直接下载 Online Installer安装会报错。用下面的具体版本安装。

sourceforge 网站 mingw-w64-release 安装文件

C运行环境配置

直接编译

直接编译.c文件:gcc -g hello_world_c.c -o hello_world_c.exe

  • -g:仅编译,生成调试信息
  • -o:指定编译得到的文件名(默认为a.out

任务配置

在VSCode菜单栏,选择【终端】->【配置任务...】,选择【C/C++: gcc.exe 生成活动文件】

则会自动在工作区文件夹生成.vscode文件夹下的tasks.json文件:

  • 注释掉"-fdiagnostics-color=always",
  • "label":表示这个任务的名称(后面在运行配置launch.json中会用到)
  • "command":任务的执行文件
    • gcc.exe解释器的路径
  • "args":执行文件后跟的参数
    • 这里其实就相当于执行命令:gcc.exe -g ${file} -o ${fileDirname}\\${fileBasenameNoExtension}.exe
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            "command": "D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "args": [
                //"-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "编译器: D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe"
        }
    ]
}

运行配置

在VSCode菜单栏,选择【运行】->【添加配置...】,选择【C++ (GDB/LLDB)】

在页面右下方点击【添加配置】->【C/C++:(gdb) 启动】

添加配置

配置生成的launch.json文件

  • "program":要运行的可执行程序
  • "externalConsole":是否需要外置的Console
  • "miDebuggerPath":设置dbg.exe路径
  • "preLaunchTask":运行前执行任务
    • 这里填上面配置的任务名("Label"值)
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe 生成活动文件"
        }
    ]
}

完成配置后,就可以直接编译运行或者调试了。

C++运行环境配置

直接编译:g++ -g hello_world_cpp.cpp -o hello_world_cpp.exe

多个task和configuration可以分别写在同一个tasks.jsonlaunch.json文件中。

tasks.json文件

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "编译器: D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe"
        }
    ],
    "version": "2.0.0"
}

launch.json文件

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\Repository\\MinGW_W64\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

多文件编译

直接编译

有文件main.chello.chello.h

  • main.c文件
#include "hello.h"
int main()
{
    printHello();
    return 0;
}
  • hello.h
#ifndef __HELLO_H__
#define __HELLO_H__
#include <stdio.h>
void printHello();
#endif
  • hello.c
#include "hello.h"
void printHello()
{
    printf("Hello world!\n");
}

对以上多个文件进行直接编译:

  • gcc *.c -o main.exe
  • gcc hello.c main.c -o main.exe

对目录下所有的.c文件进行编译

  • 在预编译阶段,GCC编译器会自行处理.h头文件,将其代码拷贝到.c文件中
0
0