参考:
1.?VSCode配置C语言环境(完整版)_ren648154292的博客-CSDN博客
2.?VScode配置C语言环境_mao_mao_chonga的博客-CSDN博客
具体的VSCode、VSCode中的C语言支持、MinGW等准备步骤上边两篇博客已经写得很详细了,在此不做赘述。只是列出来我这里实测可用的代码以及过程中遇到的问题和解决方法。
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"//解决中文乱码
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/include/**",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/include/**",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
]
}
}
],
"version": 4
}
hello.c
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("hello world\n");
system("pause");
return 0;
}
对于过程中涉及到的一些知识以及遇到的问题和解决方法,作以下说明:
1.?c_cpp_properties.json是配置C语言环境的json文件,其中“includePath”和"path"项的内容要与实际MinGW的安装路径相匹配。
2.?tasks.json用作编译,launch.json用来执行编译好的文件。
3. launch.json中的"miDebuggerPath"项要设置为"C:/工具/MinGW/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/gdb.exe"(根据实际的存放位置进行修改)。
4. 实际使用中发现两台电脑通过代码中的printf打印输出时,其中一台电脑在"TERMINAL"下输出,而另一台电脑则是新开了一个命令窗口,在命令窗口中进行输出。这是由launch.json中的"externalConsole"项控制的,如果此项的值设置为false,则是前一种情况;如果此项的值设置为true,则是后一种情况。
|