栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Windows下配置vscode运行c++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Windows下配置vscode运行c++

Windows下配置vscode运行c++

文章目录
  • Windows下配置vscode运行c++
  • 前言
  • 一、下载/安装g++、gdb
  • 二、使用以下配置
    • 1.launch.json
    • 2.tasks.json
    • 3.c_cpp_properties.json
    • 安装插件:C/C++、C/C++ Runner
    • 问题


前言

记录vscode如何运行c++程序


一、下载/安装g++、gdb

windows下如何下载g++以及gcc

二、使用以下配置

修改了一下如何使用vscode运行和调试c/c++程序中launch.json需要自行修改的部分,以下可以直接复制粘贴使用

1.launch.json

代码如下(示例):

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "(gdb) Launch",
        "type": "cppvsdbg",//膜拜https://blog.csdn.net/Dear_Atri/article/details/123057682
        "request": "launch",
        "program": "${fileDirname}\${fileBasenameNoExtension}.exe",
        "args": ["g++","/C","${fileDirname}/${fileBasenameNoExtension}.exe", "&", "pause"],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "internalConsoleOptions": "neverOpen",
        "MIMode": "gdb",
        "miDebuggerPath": "gdb.exe",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": false
          }
        ],
        "preLaunchTask": "Compile"
      },
      {
        "name": "C/C++ Runner: Debug Session",
        "type": "cppdbg",
        "request": "launch",
        "args": [
          ""
        ],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}", // 调试程序时的工作目录
        "environment": [],
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
        "internalConsoleOptions": "openOnSessionStart",
        "MIMode": "gdb",
        "miDebuggerPath": "gdb",
        "externalConsole": false,
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    ]
  }
2.tasks.json

代码如下(示例):

{
    "version": "2.0.0",
    "tasks": [{
        "label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
        "command": "g++",   // 要使用的编译器,C++用g++
        "args": [
            "${file}",
            "-o",    // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
            "${fileDirname}/${fileBasenameNoExtension}.exe",
            "-g",    // 生成和调试有关的信息
            "-m32",  // 不知为何有时会生成16位程序而无法运行,此条可强制生成64位的
            // "-Wall", // 开启额外警告
            "-static-libgcc",     // 静态链接libgcc,一般都会加上
            "-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这条会导致Win下输出中文乱码;繁体系统改成BIG5
            "-D__USE_MINGW_ANSI_STDIO", // 用MinGW写C时留着,否则不需要,用于支持printf的%zd和%Lf等
        ], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
        "type": "process", // process是把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
        "group": {
            "kind": "build",
            "isDefault": true // 不为true时ctrl shift B就要手动选择了
        },
        "presentation": {
            "echo": true,
            "reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档,即使设为never,手动点进去还是可以看到
            "focus": false,     // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
            "panel": "shared"   // 不同的文件的编译信息共享一个终端面板
        }
        // "problemMatcher":"$gcc" // 捕捉编译时终端里的报错信息到问题面板中,修改代码后需要重新编译才会再次触发
        // 本来有Lint,再开problemMatcher就有双重报错,但MinGW的Lint效果实在太差了;用Clangd可以注释掉
    }]
}
3.c_cpp_properties.json
{
    "configurations": [
      {
        "name": "windows-gcc-x64",
        "includePath": [
          "${workspaceFolder}/**"
        ],
        "compilerPath": "C:\MinGW\bin\g++.exe",//安装g++默认路径
        "cStandard": "${default}",
        "cppStandard": "${default}",
        "intelliSenseMode": "windows-gcc-x64",
        "compilerArgs": []
      }
    ],
    "version": 4
  }
安装插件:C/C++、C/C++ Runner 问题

每次使用F5运行时出现以下提示

> Executing task: C:MinGWbing++.exe f:BaiduYunDownloadc++c++test.cpp -o f:BaiduYunDownloadc++/c++test.exe -g -m32 -static-libgcc -fexec-charset=GBK -D__USE_MINGW_ANSI_STDIO <

In file included from c:mingwincludewchar.h:54,
                 from c:mingwlibgccmingw329.2.0includec++cwchar:44,
                 from c:mingwlibgccmingw329.2.0includec++bitspostypes.h:40,
                 from c:mingwlibgccmingw329.2.0includec++iosfwd:40,
                 from c:mingwlibgccmingw329.2.0includec++ios:38,
                 from c:mingwlibgccmingw329.2.0includec++ostream:38,
                 from c:mingwlibgccmingw329.2.0includec++iostream:39,
                 from f:BaiduYunDownloadc++c++test.cpp:1:
c:mingwinclude_mingw.h:413:3: warning: #warning "Direct definition of __USE_MINGW_ANSI_STDIO is deprecated." [-Wcpp]
  413 | # warning "Direct definition of __USE_MINGW_ANSI_STDIO is deprecated."
      |   ^~~~~~~
In file included from c:mingwincludewchar.h:54,
                 from c:mingwlibgccmingw329.2.0includec++cwchar:44,
                 from c:mingwlibgccmingw329.2.0includec++bitspostypes.h:40,
                 from c:mingwlibgccmingw329.2.0includec++iosfwd:40,
                 from c:mingwlibgccmingw329.2.0includec++ios:38,
                 from c:mingwlibgccmingw329.2.0includec++ostream:38,
                 from c:mingwlibgccmingw329.2.0includec++iostream:39,
                 from f:BaiduYunDownloadc++c++test.cpp:1:
c:mingwinclude_mingw.h:414:18: note: #pragma message: See <_mingw.h> for preferred feature activation methods.
  414 | # pragma message "See <_mingw.h> for preferred feature activation methods."
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

终端将被任务重用,按任意键关闭。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/870136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号