4、用vscode跑C语言代码,怎么搞

按照windows的C编译器mingw

安装后,还需要在vscode的C插件中配置,很麻烦,得仔细配,错了也跑不成。
好像也有别的插件可以替代mingw,具体可以查查。

装插件

前三个一定要装,后面的忘了为什么装了、也不知有没有用到,需要用再说吧:
C/C++ v1.20.5,不是最新版
C/C++ Extension Pack
C/C++ Themes
CMake
CMake Tools
Embedded Tools:这个是嵌入式的插件,还没用过

如何运行

(1)编译,Run Build Task...Ctrl+Shift+B

(2)打开main.c,右上角点击Debug

(3)如果点运行后,弹出.json文件,按照下面设置,launch中易出错的设置已经写了注释:

Debug时,总是弹出launch.json让设置,很烦,改了半天才能用。

主要是因为,自动生成的launch.json文件的路径和变量有问题,tasks.json中也要同步修改。【这个问题,好像切换C/C++版本到v1.20.5就可以了】

需要.vscode下的几个json文件,备份如下:

c_cpp_properties.json
{
  "configurations": [
    {
      "name": "windows-gcc-x64",
      "includePath": [
        "${workspaceFolder}/**",
        "${workspaceFolder}/src",
        "${workspaceFolder}/src/Include"
      ],
      "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
      ],
      "cStandard": "c17",
      "cppStandard": "gnu++14",
      "compilerPath": "D:/install/mingw64/bin/gcc.exe",
      "intelliSenseMode": "windows-gcc-x64"
    }
  ],
  "version": 4
}
launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: gcc.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // ${workspaceFolder}\\${workspaceFolderBasename}.exe 【默认这个有问题,找不到】   ${fileDirname}\\output\\${fileBasenameNoExtension}.exe
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", // D:/install/mingw64/bin  或 ${workspaceFolder}
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\install\\mingw64\\bin\\gdb.exe", // 你的MinGW编译器下gdb的目录,请根据实际情况调整
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        }
    ]
}
settings.json
{
  "C_Cpp_Runner.cCompilerPath": "D:/install/mingw64/bin/gcc.exe",
  "C_Cpp_Runner.cppCompilerPath": "D:/install/mingw64/bin/g++.exe",
  "C_Cpp_Runner.debuggerPath": "D:/install/mingw64/bin/gdb.exe",
  "C_Cpp_Runner.cStandard": "c17",
  "C_Cpp_Runner.cppStandard": "gnu++14",
  "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
  "C_Cpp_Runner.useMsvc": false,
  "C_Cpp_Runner.warnings": [
    "-Wall",
    "-Wextra",
    "-Wpedantic",
    "-Wshadow",
    "-Wformat=2",
    "-Wcast-align",
    "-Wconversion",
    "-Wsign-conversion",
    "-Wnull-dereference"
  ],
  "C_Cpp_Runner.msvcWarnings": [
    "/W4",
    "/permissive-",
    "/w14242",
    "/w14287",
    "/w14296",
    "/w14311",
    "/w14826",
    "/w44062",
    "/w44242",
    "/w14905",
    "/w14906",
    "/w14263",
    "/w44265",
    "/w14928"
  ],
  "C_Cpp_Runner.enableWarnings": true,
  "C_Cpp_Runner.warningsAsError": false,
  "C_Cpp_Runner.compilerArgs": [],
  "C_Cpp_Runner.linkerArgs": [],
  "C_Cpp_Runner.includePaths": [
    "${workspaceFolder}/**",
    "${workspaceFolder}/src",
    "${workspaceFolder}/src/Include"
  ],
  "C_Cpp_Runner.includeSearch": [
    "*",
    "**/*"
  ],
  "C_Cpp_Runner.excludeSearch": [
    "**/build",
    "**/build/**",
    "**/.*",
    "**/.*/**",
    "**/.vscode",
    "**/.vscode/**"
  ],
  "C_Cpp_Runner.useAddressSanitizer": false,
  "C_Cpp_Runner.useUndefinedSanitizer": false,
  "C_Cpp_Runner.useLeakSanitizer": false,
  "C_Cpp_Runner.showCompilationTime": false,
  "C_Cpp_Runner.useLinkTimeOptimization": false,
  "C_Cpp_Runner.msvcSecureNoWarnings": false,
  "files.associations": {
    "main.h": "c",
    "tpdef.h": "c",
    "globalvar.h": "c"
  },
  "editor.rename.enablePreview": false
}
tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "D:/install/mingw64/bin/gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I",
                "${workspaceFolder}\\src\\Include",
                "-I",
                "${workspaceFolder}\\**"
            ],
            "options": {
                "cwd": "D:/install/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

3、C语言硬件相关的代码不识别

  "files.associations": {
    "main.h": "c",
    "tpdef.h": "c",
    "globalvar.h": "c"
  },

使用files.associations配置项,人们可以将一个文件的扩展名手动关联到一个已支持的语言上,这样,即使VSCode无法自动识别文件类型,也能以适当的方式处理这些文件
我这里的设置是按照C语言处理,因为main.h中有一些嵌入式硬件相关的定义(中断等),vscode的C插件不认识。
不这么设置的话,总是报编译错误,就不工作了、搜索也不成。

2、vscode 大纲 找不到符号

我的解决方法,真的想不到:卸载这个插件Chinese (Simplified) (简体中文)

我只是想卸载这个插件,以看到“找不到符号”的英文是啥,好去vscode的官网去搜索,
结果,大纲直接出来了。。。
看来是翻译插件导致了大纲无法识别,
大概率是,我为了不乱码,把UTF-8改成了GBK,然后翻译插件就不行了。

讲真,以后用工具,还是尽量用英文原版吧
主要是中文环境的解决方法太少了,vscode连个中文社区都没有,还得翻译,又不准确,
不如直接用英文原版,不懂直接google查,没有查不到的。

1、vscode中文乱码

1、打开文件出现乱码
方法一、
①先“打开文件”,然后点击“首选项”,接着点击“设置”;
②接着在搜索栏中输入Files:Auto Guess Encoding ,将其勾选即可。

方法二、
搜索插件GBKtoUTF8,并将其安装。

方法三、
这是最简单的方法,点击右下角utf8,将其换成GBK。

2、终端输出出现乱码

方法一、
在终端输入 chcp 查看编码 936代表GBK,65001代表utf8,然后输入chcp 65001即可切换。

方法二、
1、依次点击“文件”–“首选项”–“设置”–“用户选项”–“功能”–“终端”–“settings.json”;
2、然后在其中加入以下代码,并点击保存:

"terminal.integrated.fontFamily": "Lucida Console",
"[cpp]": {
"files.encoding": "gbk"
},
"[c]": {
"files.encoding": "gbk"
}

参考:https://www.win7zhijia.cn/win10jc/win10_47789.html

posted on 2022-08-04 15:33  西伯尔  阅读(343)  评论(0编辑  收藏  举报