[C++] VSCode configuration c++ environment (xiaobai tutorial)

Configure the c++ environment

Preface

  • My environment: Windows10 64 bit
  • VSCode is still very convenient to use, it runs fast, occupies little system resources, and has a wealth of plug-ins

Software Installation

1. Download and install MinGW-w64 and environment variable configuration

MinGW: It can be understood as a part of the software that contains a variety of compilation environments that can be freely installed

Download address: https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/
directly pop-up window to download, after downloading, follow the tutorial to install and configure.

Insert picture description here

In view of the installation failure of many people, I put the MinGW installation file on the network disk, the link is as follows:
link: https://pan.baidu.com/s/1hng1o1ruDn11JlnQBq7xaw
extraction code: r823

installation
Insert picture description here

After the installation is complete
Insert picture description here

Download related files (important steps)

Open MinGW, check the compiler software

  • mingw32-gcc.bin (c language file compiler)
  • mingw32-gcc-g++.bin (c++ language compiler)
  • mingw32-gdb.bin (debugging the compiled file)

If the error "xxx library could not be found" is reported during installation, you can re-check this library and dll file

Insert picture description here
After selecting, click  Installation> applychange and wait for the installation to be completed.
Insert picture description here

For mingw32-gdb.bin and other download failure problems, here I put the three required files on the network disk, and after downloading, put the corresponding files that I did not download successfully in the MinGW\bin directory. The link is as follows:
Link: https://pan.baidu.com/s/1ZJFnR_MRkOTeufjKO4dLnw
Extraction code: 18rx

It’s easy to fail to download here, you can try to download using the mobile hotspot, try more; if not, download a few more times

Add environment variables

  • Right-click on  My Computer and  click  Properties
    Insert picture description here

  • Click  Advanced system settings
    Insert picture description here

  • Click on  environment variables
    Insert picture description here

  • Select Path in the system variables  , create a new
    Insert picture description here

  • Add the  bin address  to the environment variable (mine is "D:\MinGW\bin")
    Insert picture description here

  • Check whether the installation is successful: enter gcc -v in the command prompt   , if the version number is displayed, the installation is successful
    Insert picture description here

 

2. Download and install VSCode

Download address: https://code.visualstudio.com/Download

  • Insert picture description here
    Insert picture description here

Configuration process

1. VSCode plugin installation

After the installation is complete, open the software

  • Download Chinese plug-in
    Insert picture description here

  • Download  c/c++  plugin
    Insert picture description here

    Restart  VSCode after the plug-in installation is complete 

2. Create a workspace

  • Create a new folder (place c++ code files)
  • Click the file to open the created folder (shortcut key Ctrl+k Ctrl+O)

Insert picture description here

3. Configuration file

Create a new .vscode folder in this folder
Insert picture description here

Create three new files in the .vscode folder

  • c_cpp_properties.json
  • launch.json
  • tasks.json
    • Insert picture description here
  • Copy the following code into the file

    Be sure to modify the file path according to your installation location

    • c_cpp_properties.json

      Enter under "includePath" path acquisition method cmd:

 gcc -v -E -x c++ -

Insert picture description here
Insert picture description here
Modify  "includePath"  and  "Path"
to replace the path in these lines as shown in the figure.

"D:/mingw/include/**" This path can also be modified and added according to your path

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "d:/mingw/include/**",
                "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++",
                "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++/mingw32",
                "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++/backward",
                "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include",
                "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../include",
                "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include-fixed"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "d:/mingw/include/**",
                    "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++",
                    "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++/mingw32",
                    "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include/c++/backward",
                    "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include",
                    "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../include",
                    "d:/mingw/bin/../lib/gcc/mingw32/8.2.0/include-fixed"
                ]
            }
        }
    ],
    "version": 4
}
  • Launch.json
    modify "miDebuggerPath" according to your own path 
{  
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示  
            "type": "cppdbg",       // 配置类型,这里只能为cppdbg  
            "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)  
            "program": "${workspaceFolder}/exe/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径  
            "args": [],             // 程序调试时传递给程序的命令行参数,一般设为空即可  
            "stopAtEntry": false,   // 设为true时程序将暂停在程序入口处,一般设置为false  
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录  
            "environment": [],  
            "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台  
            "MIMode": "gdb",  
            "miDebuggerPath": "D:/MinGW/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应  
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc  
            "setupCommands": [  
                {   
		    "description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ]  
        }  
    ]  
}

Just
copy and paste tasks.json directly without modification

Here I changed the path of the exe by the way. In "args", if you need to modify it, you can Baidu by yourself. Here, mine will put the compiled exe file separately in an exe folder

Insert picture description here

{
    "version": "2.0.0",
    "command": "g++",
    "args": [
        "-g",
        "${file}",
        "-o",
        "${workspaceFolder}/exe/${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
        }
    }
}

Here is an explanation of the custom exe file storage path in args:
${workspaceFolder}: current working path
exe: self-built folder
${fileBasenameNoExtension}: the file name of the current file, NoExtension means no suffix, and then add one. exe is the current exe file, which is the compiled exe file.
Putting the above paths together means that the compiled exe file will be placed in the exe folder. If this configuration is deleted, the compiled exe file will be placed in the current by default Under the working path.

By the way, what do some variables in vscode mean?
${workspaceFolder}: Represents the path of the current workspace folder, that is, /home/Coding/Test
${workspaceRootFolderName}: Represents the folder name of the workspace, that is, Test
${file}: The absolute path of the file itself, that is, /home/Coding/Test/.vscode/tasks.json
${relativeFile}: the path of the file in the workspace, that is, .vscode/tasks.json
${fileBasenameNoExtension}: the file of the current file Name without suffix, that is, tasks
${fileBasename}: the file name of the current file, tasks.json
${fileDirname}: the path to the folder where the file is located, that is, /home/Coding/Test/.vscode
${fileExtname} : The suffix of the current file, that is. json
${lineNumber}: The line number where the cursor of the current file is located
${env:PATH}: Environment variables in the system

4. Testing

 

Reprinted: https://ruochen.blog.csdn.net/article/details/104096661

Guess you like

Origin blog.csdn.net/Zhouzi_heng/article/details/115014059