之前谈到过vscode的C/C++配置的问题,但是使用之后不久会发现会找不到.exe文件并且终端运行之后会出现闪退,然后我研究了一下也查阅了相关的资料,并将最终的配置给出。
首先我在桌面上新建了一个空白的文件夹"Code",然后在Code中又新建了三个文件夹,分别是".vscode"里面放配置文件,"C"里面装C程序,"C++"里面装C++程序,如下图所示。
然后在.vscode中新建四个文件
c_cpp_properties.json
{
"configurations": [
{
"name": "Win64",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/MinGW-W64 GCC/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json
{
"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-W64 GCC\\mingw64\\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
}
}
],
"presentation": {
"panel": "new" //默认为“shared“表示共享,改成new之后每个进程创建新的端口
}
}
运行的过程中会发现,出现端口被占用然后打不开终端的问题,所以我在tasks.json最后加上了presentation,表示每个进程创建新的端口。
然后运行可能会出现能打开终端但是会出现闪退的情况,这时候在我们写的代码下面加上"getchaer()"就可,下面给出2-1000内的质数快去测试试试吧~
#include <stdio.h>
int main()
{
int i = 0, j = 0;
for (i = 3; i < 1000; i++)
{
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
break;;
}
}
if (i == j)
{
printf("%d ", i);
}
}
getchar();
return 0;
}