Visual Studio Code configuration C/C + + compilation environment process and problem solving (Win10 environment)

Posted by FamousMortimer on Sat, 19 Feb 2022 23:30:26 +0100

1. Introduction to Visual Studio Code

VS Code is actually a powerful text editor, which can also realize the compilation function on the basis of installing plug-ins. Support Windows, OS X and Linux, with built-in JavaScript, TypeScript and node JS support, and has a rich plug-in ecosystem. You can install plug-ins to support C + +, C#, Python, PHP and other languages.

VS Code official website:

https://code.visualstudio.com/docs/cpp/config-mingw#_prerequisites

2. Visual Studio Code installation

VS is relatively simple. After downloading the installation package from the official website, click next step according to the process.

Download link on the official website:

https://code.visualstudio.com/download

3. Visual Studio Code configuration C/C + + compilation environment

3.1. Install the C/C + + running environment plug-in on VS

3.2. Download MinGW plug-in and configure environment variables

3.2.1 download link

Download address: https://sourceforge.net/projects/mingw-w64/files/

Downloaded files: after entering the website, do not click "download last version", slide down and find the latest version of "x86_64 POSIX sEH".

Install MinGW: after downloading, it is a 7z compressed package. Unzip it and move it to the location you want to install.

Note: it's better not to use Chinese name for the path of installation package to avoid other unexpected problems.

3.2.2 setting environment variables

Control panel -- > system and security -- > System -- > advanced system configuration -- > Advanced -- > environment variable -- > system variable -- > path, add the path of bin directory in MinGW installation package:

For example: D:\Program Files\mingw64\bin\

3.3. Configure VS C/C + + running environment

3.3.1 configuration c_cpp_properties.json file

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "D:/Program Files/mingw64/x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}",
            "compilerPath": "D:/Program Files/mingw64/bin/g++.exe"
        }
    ],
    "version": 4
}

3.3.2. Configure launch json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(g++)Start",
            "preLaunchTask":"g++",//With tasks The label in JSON is consistent
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,//false change to true
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\Program Files\\mingw64\\bin\\gdb.exe",//"/path/to/gdb"
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true,
                }
            ]
        }
    ]
}

3.3.3. Configure tasks json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++",
            "type": "shell",
            "command":"D:\\Program Files\\mingw64\\bin\\x86_64-w64-mingw32-g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\Program Files\\mingw64\\bin"
            },
            "problemMatcher":[
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
        },
    ]
}

Note: launch The pre launch task tag in JSON should be configured with tasks The configuration of the label tag in JSON is the same. The above two files are set to "g + +".

3.3.4 reference link:

https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

https://www.cnblogs.com/bpf-1024/p/11597000.html

4. Problems and solutions when configuring C/C + + compilation environment with Visual Studio Code

4.1. Problem Description: vscode C++ debug: lldb: unrecognized option `--interpreter=mi 'or unable to start debugging Launch options string provided by the project system is invalid. Unable to determine path to debugger. Please specify the "MIDebuggerPath" option.

(1) Cause of problem:

launch. There is a problem in the configuration of debuggerpath.

(2) Solution

 "MIMode": "gdb",
 "miDebuggerPath": "D:\\Program Files\\mingw64\\bin\\gdb.exe",//"/path/to/gdb"

Note: GDB is required The full path of exe. gcc.exe cannot be used Exe or G + + Exe, etc.

4.2. Problem Description: vscode has detected #include error, please update includePath

(1) execute g++ -v -E -x c + + under CMD-

C:\Users\z00017938>g++ -v -E -x c++ -
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_64-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
COLLECT_GCC_OPTIONS='-v' '-E' '-shared-libgcc' '-mtune=core2' '-march=nocona'
 D:/Program Files/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/cc1plus.exe -E -quiet -v -iprefix D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/ -D_REENTRANT - -mtune=core2 -march=nocona
ignoring duplicate directory "D:/Program Files/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++"
ignoring duplicate directory "D:/Program Files/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32"
ignoring duplicate directory "D:/Program Files/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward"
ignoring duplicate directory "D:/Program Files/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include"
ignoring nonexistent directory "C:/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../include"
ignoring duplicate directory "D:/Program Files/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed"
ignoring duplicate directory "D:/Program Files/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
ignoring nonexistent directory "C:/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include
End of search list.

(2) copy the following path

 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed
 D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include

(3)vscode adds the copied path to c_cpp_properties.json is located as follows:

"includePath": [
                "${workspaceFolder}/**",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "D:/Program Files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "D:/Program Files/mingw64/x86_64-w64-mingw32/include"
            ],

 

 

Topics: C++ Visual Studio Code mingw