VSCode配置C和C++语言

Mingw

1.首先我们需要安装一下Mingw

大概目录结构如下

 2.然后我们解压后就看到上面的一个页面,这时我们需要配置一下环境变量。

在用户变量和系统变量都可以,只要是在path下就行。

3.现在我们可以在cmd下测试一下。

出现类似以下就代表成功了。

现在我们再输入gcc -v -E -x c++ -,然后我们会出现下面的东西。

 我们可以看到 这几个路径,很重要。

VSCode操作

配置c语言

然后我们应该已经安装好vscode了。打开之前我们要确定一下代码路径没有出现中文。(这个很重要)

首先创建一个空的文件夹,在cscode中打开它。我们可以写一个c语言文件。

 接下我们首先要安装几个插件。

5.然后我们只需要配置一下文件即可。

 c_cpp_properties.json文件

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                //mingw安装位置的include
                "E:\\Mingw64\\include\\**",
                //在终端运行gcc -v -E -x c++ -得到下面
                "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "E:/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}",
                    //mingw安装位置的include
                    "E:\\Mingw64\\include\\**",
                    //在终端运行gcc -v -E -x c++ -得到下面
                    "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "E:/Mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

 launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(Windows) Launch",//windows终端版
      "type": "cppvsdbg",
      "request": "launch",
      "program": "cmd",
      "preLaunchTask": "echo",
      "args": [
        "/C",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "&",
        "echo.",
        "&",
        "pause"
      ],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true
    },
    {
      "name": "(gdb) Launch",//本地版
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",//运行的程序
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,//false表示用vscode运行,true表示用cmd终端运行。
      "MIMode": "gdb",
      "miDebuggerPath": "E:/Mingw64/bin/gdb.exe",//mingw的安装位置
      "preLaunchTask": "echo",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    },
    {
      "name": "C/C++ Runner: Debug Session",
      "type": "cppdbg",
      "request": "launch",
      "args": [],
      "stopAtEntry": false,
      "externalConsole": true,
      "cwd": "e:/vscode代码/c/demo2",
      "program": "e:/vscode代码/c/demo2/build/Debug/outDebug",
      "MIMode": "gdb",
      "miDebuggerPath": "E:/Mingw64/bin/gdb.exe",//mingw的安装位置
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

 tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "E:/Mingw64/bin/gcc", //mingw安装位置
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK" //解决中文乱码
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
        "showReuseMessage": true,
        "clear": false
    }
}

配置c++

基本也都不用变

仅需修改一下launch.json和takes.json文件即可

launch.json

{  
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
            "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],  
            "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
            // 这里的路径需要修改。改成自己的路径
            "miDebuggerPath": "E:/mingw64/bin/gdb.exe",
 
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
            "setupCommands": [  
                {   
		    "description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }  
    ]  
}

takes.json

{
    "version": "2.0.0",
    "command": "g++",
    "args": [
        "-g",
        "${file}",
        "-o",
        "${workspaceFolder}/${fileBasenameNoExtension}.exe"
    ], // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "\\"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_53011574/article/details/126728451
今日推荐