vscode配置c/c++环境
VS Code 配置 C/C++ 编程运行环境(保姆级教程)_vscode配置c++环境-CSDN博客
1. 安装vscode,安装插件(C/C++ Runner C/C++ Build Task C/C++ C/C++ Extension Pack CMake CMake Tools)
2. 安装MinGW-W64,添加到环境变量,mingw32-make.exe重命名为make
gcc --version g++ --version gdb --version
main.c
#include <stdio.h> #include "max.h" int main() { for (int i = 0; i < 2; i++) printf("Hello Grayson~%d\n", i); int a = 10; int b = 30; int c = findMaxNum(a, b); printf("%d\n", c); return 0; }
max.c
#include "max.h" int findMaxNum(int num1, int num2) { return num1 > num2 ? num1 : num2; }
max.h
#ifndef __MAX_H__ #define __MAX_H__ #include <stdio.h> int findMaxNum(int num1, int num2); #endif // __MAX_H__
CMakeLists.txt
# 指定CMake最低版本要求
cmake_minimum_required(VERSION 3.10)
# 定义项目名称
project(helloworld)
# 设置C标准
set(CMAKE_C_STANDARD 11)
# 生成可执行文件,列出所有源文件
add_executable(helloworld main.c max.c max.h)
测试
在VSCode中配置一个构建任务。
-
按
Ctrl+Shift+P,输入并选择 “任务:配置任务”。 -
选择 “从模板创建 tasks.json 文件” -> “Others”。
-
这会创建一个
.vscode/tasks.json文件,将其修改为调用make:{ "version": "2.0.0", "tasks": [{ "label": "build with make", "type": "shell", "command": "make", "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] }] }
配置后,按 Ctrl+Shift+B 即可执行 make 命令来构建项目
发布
使用 CMake 自动生成 Makefile
Makefile编译
F:\demo2025\c\helloworld>rmdir /s /q build F:\demo2025\c\helloworld>mkdir build F:\demo2025\c\helloworld>cd build F:\demo2025\c\helloworld\build>cmake -G "MinGW Makefiles" .. -- The C compiler identification is GNU 15.2.0 -- The CXX compiler identification is GNU 15.2.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/Program_Files/mingw64/bin/cc.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: D:/Program_Files/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done (8.6s) -- Generating done (0.0s) -- Build files have been written to: F:/demo2025/c/helloworld/build F:\demo2025\c\helloworld\build>make [ 33%] Building C object CMakeFiles/helloworld.dir/main.c.obj [ 66%] Building C object CMakeFiles/helloworld.dir/max.c.obj [100%] Linking C executable helloworld.exe [100%] Built target helloworld F:\demo2025\c\helloworld\build>helloworld.exe Hello Grayson~0 Hello Grayson~1 30
qq:505645074

浙公网安备 33010602011771号