注:仅为备份
1. 配置 C++ 编译器
首先需要安装 C++ 编译器。在 Windows 上,可以安装 MinGW 或者 Visual Studio;在 MacOS 上,可以安装 Clang 或者 Xcode;在 Linux 上,可以安装 GCC。
以 Windows 的 MinGW 为例:
1.1 下载 MinGW
官方下载:https://www.mingw-w64.org/downloads/
sourceforge下载:https://sourceforge.net/projects/mingw-w64/files/
(1)进入 sourceforge 网页,点击 x86_64-win32-seh
进行下载:
(2)解压该压缩包(x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z
),解压之后会出现一个 mingw64
文件夹:
1.2 添加环境变量
win + i
调出系统设置,在查找设置中输入环境变量,点击编辑账户的环境变量,将 mingw64
下的 bin
路径( D:\PATH\bin
)添加至用户变量:
2. 配置编辑器 VSCode
2.1 安装插件
C/C++
:
3. 优化
目录结构
test.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World!\n");
printf("你好世界!\n");
system("pause"); // 防止运行后自动退出,需头文件stdlib.h
return 0;
}
test.cpp
#include <iostream>
int main(){
std::cout << "Hello, world!" << std::endl;
system("pause");
return 0;
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "D:\\PATH\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\build\\${fileBasenameNoExtension}.exe", /*build文件夹收纳exe可执行文件*/
"-fexec-charset=GBK",
"-std=c++17"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\build\\${fileBasenameNoExtension}.exe", /*build文件夹收纳exe可执行文件*/
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\PATH\\mingw64\\bin\\gdb.exe", /*bin目录下的gdb.exe,两个反斜杠\\*/
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:\\PATH\\mingw64\\bin\\g++.exe", /*bin目录下的g++.exe,两个反斜杠\\*/
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}