在vscode 中运行c++
环境搭建
搜索安装 C/C++ 和 code runner 插件
下载mingb64,解压,配置环境变量
在这个 .vscode 文件夹下新建2个文件作为配置文件(launch.json和tasks.json)
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
32
33{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Program Files\\mingw64\bin\\gdb.exe",
//**********要修改为自己的编译器所在的bin路径,形如 c:\\***\\bin\\gdb.exe
"preLaunchTask": "gc",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}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{
"version": "2.0.0",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}按ctrl+shit+p,搜索找到编辑配置(JSON)文件,并打开检查自己的“includePath”
搜索 c/c++, 编辑配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
]
}
],
"version": 4
}g++ -v -E -x c++ -,进行查看路径并复制下来
上边里面D盘路径都是这个命令查出来的
重启vscode
编写示例 hello.c
1
2
3
4
5
int main(void){
printf("Hello World!");
return 0;
}运行 runCode
常用基本类型
- short,int,long 整形
- float,double 浮点型
- char 字符串
- void
数组
有序的元素序列
1 | // 字符数组 |
结构体
类似于对象的概念
1 | struct { |
枚举类型
1 | enum em { |
指针
储存地址的变量
1 | // 定义指针 |
堆内存的分配于释放
1 | // 分配内存 |
1 |
|
函数
函数定义没有提升,只能写在调用之前
1 | void func (int a) { ...} |
函数指针
返回值类型(*指针变量名)([形参列表])
1 | // 声明函数指针 |
操作文件
通过 FILE* file;
打开文件
1 | FILE* fopen(path, mode); |
关闭文件
1 | fclose(FILE*); |
- 本文作者: 王不留行
- 本文链接: https://wyf195075595.github.io/2023/10/31/programming/c/base/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!