

Vscode配置C/C++环境
出现的问题
问题1:gcc : 无法将“gcc”项识别为 cmdlet、函数、脚本文件或可运行程序的名称,
调试方法:直接在cmd执行gcc test.c -o test 然后再执行test成功输入helloworld //这句编译名为test.c的源文件,并将输出的可执行文件命名为 test,
解决:右击Vscode以管理员身份运行,或右击vscode--属性--兼容性,勾上以管理员身份运行此程序
问题2:undefined reference to `WinMain', collect2.exe: error: ld returned 1 exit status
原因:代码没有保存,完成代码输入后要保存,再编译运行才可以
问题3:The preLaunch Task 'C/C++: gcc.exe build active file' terminated with exit code -1.
1. 安装vscode
2. 到https://nuwen.net/mingw.html#install下载mingw-18.0-without-git.exe并解压到C盘
3. 将g++所在bing目录的路径C:\MinGW\bin添加到系统的环境变量, //到cmd下输入g++ --version(注:这里的--version是两个--)如出现字符表配置成功
4. 在Vscode扩展里安装C/C++ INtelliSense那个扩展和code Runner扩展
//code Runner扩展的好处是安装它后在调试C/C++代码时,可以只选一段来调试,要调试那段,只需用鼠标选择那段
5. D盘新建文件夹VScode,再用vscode软件打开该文件夹,打开时选择信任父级,在vscode软件里再在该目录下新建.vscode文件夹
5. 再用vscode软件在.vscode目录下新建四个文件,c_cpp_properties.json、launch.json、settings.json、tasks.json
6. 分别打开四个文件,粘贴如下内容
c_cpp_properties.json文件的内容:
注:compilerPath这里改为自己的
{
"configurations": [
{
"name": "Win64",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/MinGW/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json文件的内容:
注:miDebuggerPath这里改为自己的
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
settings.json文件的内容:
{
"files.associations": {
"*.py": "python",
"iostream": "cpp",
"*.tcc": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"ostream": "cpp",
"new": "cpp",
"typeinfo": "cpp",
"deque": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"fstream": "cpp",
"sstream": "cpp",
"map": "c",
"stdio.h": "c",
"algorithm": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"ios": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"memory": "cpp",
"random": "cpp",
"set": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"stdlib.h": "c",
"string.h": "c"
},
"editor.suggest.snippetsPreventQuickSuggestions": false,
"aiXcoder.showTrayIcon": true
}
tasks.json文件的内容:
{
"version": "2.0.0",
"tasks": [
{
"label": "g++",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
注:C/C++代码必须放在有.vscode的目录,注文件名和路径中不能有中文
7. 右击Vscode以管理员身份运行,或右击vscode--属性--兼容性,勾上以管理员身份运行此程序就可以调试C程序了
8. 以下代码测试成功,输出不能有中文,否则乱码
测试代码
test.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "hello, world!" << endl;
return 0;
}
test.c
#include <stdio.h> //include称为文件头文件包命令
void main() //每个C源程序有且只有一个主函数main
{
printf("hello,world!!\n"); //printf是调用的一个函数,调用这个函数是不需要引入头文件的
//使用中文要用双引号括起来,C只支持英文字符
}
--最后选择要运行的代码右击--Run Code就可以了
9. 解决中文输出乱码,见"D:\SynologyDrive\Learn\Html与CSS\VSCode\VSCode打开文件,运行输出乱码.txt"
解决方法:
方法一:将源码从utf8改为gbk保存。打开test2.c,在vscode右边的编辑窗口,点击右下角的utf8,再点击通过编码保存,点击简体中文(GBK),然后utf8那里就会变成gbk,重新运行就正常了
注1:有时这样操作还是乱码,但新建一个Test3.c复制代码进去再运行就可以了
注2:用鼠标选择一段代码运行会乱码,但将这段代码复制到一个单独文件运行不乱码,原因code Runner扩展不支持中文
方法二:如果是没有windows窗口的命令行程序,可以直接在调试区的输出那里显示。点击vscode左下的设置,输入run in terminal,把whether to run code in integrated terminal前的勾去掉即可。
可以先自己在命令行下用gcc test2.c -o test2编译查看一下看是否正常
来自:https://blog.csdn.net/m0_62721576/article/details/127207028