目录

VSCode-C语言环境多文件一键运行

VSCode C语言环境(多文件一键运行)

配置CMake

下载CMake并安装

https://cmake.org/download/

配置mingw64

https://github.com/niXman/mingw-builds-binaries/releases
i686-14.2.0-release-mcf-dwarf-ucrt-rt_v12-rev1.7z
i686-14.2.0-release-posix-dwarf-msvcrt-rt_v12-rev1.7z
i686-14.2.0-release-posix-dwarf-ucrt-rt_v12-rev1.7z
i686-14.2.0-release-win32-dwarf-msvcrt-rt_v12-rev1.7z
i686-14.2.0-release-win32-dwarf-ucrt-rt_v12-rev1.7z
x86_64-14.2.0-release-mcf-seh-ucrt-rt_v12-rev1.7z
x86_64-14.2.0-release-posix-seh-msvcrt-rt_v12-rev1.7z
x86_64-14.2.0-release-posix-seh-ucrt-rt_v12-rev1.7z
x86_64-14.2.0-release-win32-seh-msvcrt-rt_v12-rev1.7z
x86_64-14.2.0-release-win32-seh-ucrt-rt_v12-rev1.7z

这里有很多版本:

  • x86_64i68664位32位 的区别, sehdwarf 分别专用于此
  • posixwin32线程模型 的区别, posix 跨平台, win32 性能好
  • ucrtmsvcrt运行时库 的区别, ucrt 只有 win10+ 支持

现在都是win10以上的系统了,肯定选 x86_64-14.2.0-release-posix-seh-ucrt-rt_v12-rev1.7z

下载解压后,将 bin 文件夹添加到环境变量,其中包括了 gccmake

配置VSCODE

安装 CMake Tools 插件

在工作区创建两个源文件

main.c

#include <stdio.h>
extern void test_cmake();
int main(int argc, char const *argv[])
{
    test_cmake();
    printf("Hello, World!\n");
    return 0;
}

test_cmake.c

#include "stdio.h"
void test_cmake()
{
    printf("Hello from test_cmake\n");
}

在工作区创建CMakeLists.txt,输入如下内容后,左下角就会生成调试和运行按钮

cmake_minimum_required(VERSION 3.10)

# Set the project name
project(TEST_C)

# Add all .c files in the current directory
file(GLOB SOURCES "*.c")

# Add an executable
add_executable(main ${SOURCES})