Vscode配置C++运行环境(2019/1//11更)并加入bits/stdc++.h头文件

因为重装系统,以前配置好的c++环境又没了。所以有要配置一遍。也更加熟悉了配置方法。

1 下载mingw64或minw, 配置好环境变量:C://mingw64//bin;  在cmd下用g++ -v验证是否成功.

2. 打开Vscode进行配置:

1. 新建文件夹C++Code,并且再建立一个bulid文件夹来放要写的代码。

Vscode配置C++运行环境(2019/1//11更)并加入bits/stdc++.h头文件_第1张图片

 

 2. 在Vscode里新建.vscode文件夹,建立上图的3个文件(setting不需要) ,文件内容如下。

 

2.1 Launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",                 // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg",                           // 配置类型,这里只能为cppdbg
            "request": "launch",                        // 请求配置类型,可以为launch(启动)或attach(附加)
            "targetArchitecture": "x64",                // 生成目标架构,一般为x86或x64
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 重点!将要进行调试的程序的路径
            "args": [],                                 // 程序调试时传递给程序的命令行参数,一般设为空即可
            "stopAtEntry": false,                       // 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceRoot}",                  // 调试程序时的工作目录,一般为${workspaceRoot}
            "externalConsole": true,                    // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "internalConsoleOptions": "neverOpen",      // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡",
            "MIMode": "gdb",                            // 指定连接的调试器
            "miDebuggerPath": "C:/Mingw64/bin/gdb.exe", //重点! 调试器路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for GDB",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile" 
        }
    ]
}

 主要改: “miDebuggerPsth”.  换成自己下载mingw路径。我鑫下的时mingw64,路径为:C:/mingw64/bin/gdb.exe

2.2  tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile",
            "command": "g++",
            "args": [
                "${file}",   //指定编译源代码文件
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe", //重点! 指定输出文件名,不加该参数则默认输出a.exe
                "-ggdb3",   // 生成和调试有关的信息
                "-Wall",    // 开启额外警告
                "-static-libgcc",   // 静态链接
                "-std=c++17",       // 使用c++17标准
                "-finput-charset=UTF-8", //输入编译器文本编码 默认为UTF-8
                "-fexec-charset=GBK",   //输出exe文件的编码
                "-D _USE_MATH_DEFINES"
            ],

            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "absolute",
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },

            "type": "shell",
            
            "group": {
                "kind": "build",
                "isDefault": true
            },

            "presentation": {
                "echo": true,
                "reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never
                 "focus": false,
                 "panel": "shared" // 不同的文件的编译信息共享一个终端面板
            },
        }
    ]
}

 

2.3 c_cpp_properties.json

{
    "configurations": [
        {
            "name": "MinGW64",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:/Mingw64/bin/g++.exe", //重点! g++.exe所在路径
            "includePath": [  //重点!
                "${workspaceFolder}",
                "C:/Mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/**",
                "C:/Mingw64/x86_64-w64-mingw32/include/**"
            ],

            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "C:/Mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/**",
                    "C:/Mingw64/x86_64-w64-mingw32/include/**"
                ]
            },
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": "",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

 这里重点该Path和includepath,我目前的为:

"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/**",

"C:/mingw64/x86_64-w64-mingw32/include/**"

 

3.  配置好了后就可以按F5运行代码,按ctrl+F5进行调试(代码写在build文件夹下);

 

3.  之后我用万能头文件写了一段代码,但是却被报错说找不到该头文件。解决办法如下:

进入bits文件夹,我的为:C:\mingw64\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\bits

在该文件夹下新建一个文本文件,复制以下代码进去。保存并改文件名为stdc++.h。

// C++ includes used for precompiling -*- C++ -*-
 
// Copyright (C) 2003-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
 
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
 
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// .
 
/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */
 
// 17.4.1.2 Headers
 
// C
#ifndef _GLIBCXX_NO_ASSERT
#include 
#endif
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#if __cplusplus >= 201103L
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif
 
// C++
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#if __cplusplus >= 201103L
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif

之后再用便没问题了。

解决方法原链接:https://blog.csdn.net/dragon60066/article/details/56529077

你可能感兴趣的:(CS)