VScode configuration C++ environment under windows system
Notice! ! ! ! ! , don’t read this article first, first read two : (2) Configure VScode under windows, write C++, use opencv library and openMVG dependent library: under windows, VSCode uses opencv https://blog.csdn.net/weixin_43686259/article/ details/129021345 ;
Because the method in this article has some problems when it is used later, after reading (2) configuring opencv, after reading (1) ( only viewing, not operating ), you will be familiar with the logic of vscode for C++ programming.
A brief description of the background
It stands to reason that Visual Studio Code is just an editor without a compiler and linker. It is more convenient to write C++ directly with Visual Studio (IDE, integrated development environment, integrated editor, compiler and linker), but I need to use Windows system and Ubuntu system, and the official Visual Studio does not directly provide a download link for the Linux version. So I finally chose vscode. Microsoft officially provides recommendations for selecting tools based on projects: https://visualstudio.microsoft.com/zh-hans/downloads/
premise
Download the Visual Studio Code (VScode) corresponding to the system: https://code.visualstudio.com/download ;
download other tools, such as Visual Studio: https://visualstudio.microsoft.com/zh-hans/downloads/
Configure VScode under windows to build a C/C++ environment
Configuration logic:
- Add gcc compilation function and gdb debugging function (using MinGM, there are two methods here, solution one is to directly use the mingw-w64 installation package; solution two, use Msys2 to install mingw-w64)
- Install
- Add environment variables
- Verify that the installation was successful
- Add extension plug-ins in VScode (convenient to write C++ code)
- Add a configuration file to the .cpp code path
- tasks.json
- launch.json
- c_cpp_properties.json
1. Add gcc compilation function and gdb debugging function
Solution 1: Use MinGW
- Download and install MinGW (The mingw-w64 project is a development environment that supports gcc compilation and gdb debugging functions, and is used to support native binary files for Windows 64-bit and 32-bit operating systems.): https://sourceforge.net/projects/mingw -w64/files/Toolchains targeting Win32/Personal Builds/mingw-builds/installer/mingw-w64-install.exe
-
Install MinGW_s
-
Parameter selection
Reference article: https://blog.csdn.net/qq_45807140/article/details/112862592- version is the version, generally choose the highest version;
- architecture is the system architecture, if the computer system is 64-bit, choose x86_64, if it is 32-bit, choose i686;
- threads is an operating system interface protocol. If you want to develop Windows programs, you need to choose win32, and if you want to develop programs under other operating systems such as Linux, Unix, and Mac OS, you need to choose posix
- exception is an exception handling model, recommend seh (64-bit system) or dwarf (32-bit system)
- build revision, the default value is enough.
I originally wanted to use MinGW-W64 directly, but the installation package I downloaded always said that the downloaded package was wrong, so I changed to the second solution.
Refer to the next steps: https://blog.csdn.net/qq_45807140/article/details/112862592、
-
Solution 2: Use Msys2 recommended by the official website (recommended this)
-
Reference address
Official tutorial address: https://code.visualstudio.com/docs/cpp/config-mingw
msys2 download address (official): https://www.msys2.org/
My Chinese installation reference: https:// blog.csdn.net/qq_50813669/article/details/124461328 -
The installation process
After the installation is complete, a black box will pop up, and there are many more icons in the menu bar:
Input:
pacman -Syu
Input:Y
(If the black box disappears after entering Y, find MSYS2 MSYS in the menu bar, or you can find it in the installation directory, double-click, the black box will pop up, re-enter, andpacman -Syu
update the remaining library)
MSYS2 has been configured, Now download mingw-w64 GCC to compile, enter the commandpacman -S --needed base-devel mingw-w64-x86_64-toolchain
to download
and wait for the download to complete
Close the black box, check the installation folder of msys64, and check whether there are the following files. If there is, it means that gcc has been downloaded successfully.
Add environment variables
to proceed further—to verify whether it can be used: presswindows+R
, enter cmd, and after the black box pops up, enterg++ --version
;gdb --version
;gcc -v
;g++ -v
etc. to view the version information.
2. Add extension tools in vscode: c/c++
Other extensible plug-ins can be added as needed: Code Runner (code runner); Tabnine (AI automatic code filling); Chinese (change the interface from English to Chinese)
3. Generation of configuration files
1. Compilation configuration of c++ program (tasks.json configuration)
Open a file directory, create a new .cpp file
and enter the test code:
```
#include
int main()
{
std::cout << "Hello World" << std::endl;
}
```
Build (compile): Click Terminal——> run build task——>c/c++:g++.exe build active file/generate active file—>generate.vscode/tasks.json file.vscode/
tasks.json File (this file is generated by myself, if it is copied from someone else, the path in the code needs to be modified to my own):
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:/msys64_vscodeUseC/msys64/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:/msys64_vscodeUseC/msys64/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
2. Program running configuration (launch.json file configuration)
点击RUN——>Add Configuration——>点击node.js——>生成./vscode/launch.json文件
./vscode/launch.json file
```
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
```
Note: This configuration file is generated by yourself after following the steps. If it is copied from someone else, the route in the program needs to be modified;
3. For the configuration of c/c++ related extensions,
press Ctrl+Shift+P to open the global search --> C/C++: Edit configurations (UI) --> modify the parameters.
At this time, c_cpp_properties.json appears in the .vscode folder file, the content is as follows:
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "D:/msys64_vscodeUseC/msys64/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
4. The configuration is successful:
Reminders from others:
- https://blog.csdn.net/m0_37605642/article/details/115654539
- Before generating task.json and launch.json files, be sure to switch to .cpp or .c files, otherwise the automatically generated task.json and launch.json cannot compile the corresponding .cpp and .c;
- It is recommended to use different folders for different programming languages;
- Make a backup copy of the .vscode folder, and copy it directly when needed in the future, without spending time on configuration;
- https://blog.csdn.net/qq_45807140/article/details/112862592
- Because vscode will exit after executing the program, you can choose to add a system ("pause") or add a sentence of getchar();
in addition, change the value of the "externalConsole" item in the launch.json file to "true", you can See external console (default is false, use integrated console)
- Because vscode will exit after executing the program, you can choose to add a system ("pause") or add a sentence of getchar();
reference
- Use Visual Studio to edit and debug Linux programs on windows: https://blog.csdn.net/qq_37233070/article/details/123454416
- Using Microsoft's official method: https://blog.csdn.net/qq_50813669/article/details/124461328
- It is said that visual studio is installed under the ubuntu system, but the installation package seems to be VScode: https://blog.csdn.net/Fzc_Ztt/article/details/117000041 (not verified)
- Directly download some URLs of MinGW: https://sourceforge.net/projects/mingw/ ; https://blog.csdn.net/Zhouzi_heng/article/details/115014059 (given the network disk address); https://osdn .net/projects/mingw/downloads/68260/mingw-get-setup.exe/ ; https://sourceforge.net/projects/mingw-w64/
- Download the official website address of Visual Studio Code: https://code.visualstudio.com/download
- Generated configuration code backup: https://blog.csdn.net/m0_37605642/article/details/115654539
- Program debugging method + knowledge about c compilation + need vcpkg and zmq (what is the role of this?): https://sourceforge.net/projects/mingw-w64/
- How to use VScode to call third-party libraries: https://blog.csdn.net/Wannna/article/details/105109375
- Introduced the configuration of compiler, editor, linker, linker and IDE and opencv: https://blog.csdn.net/m0_37833142/article/details/105686820