Every time you change computers to write C++, you have to find the configuration, which is very cumbersome. This time I wrote a blog and recorded the relevant configuration.
Install the compiler
Open the website below http://www.msys2.org/
, download the 64-bit version MYSYS2
, and follow the steps on the home page to install the software. After the completion, enter the /etc/pacman.d
directory and modify the following three files respectively.
The content of the mirrorlist.mingw32 file is:
Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/
The content of the mirrorlist.mingw64 file is:
Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/
The contents of the mirrorlist.msys file are:
Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/
Then allow the following command to update the system.
cd ~
pacman -Syu
Install the c++ tools.
pacman -S mingw-w64-x86_64-toolchain
A prompt appears, select 3, 9, and 13 respectively. Wait for the installation to complete.
After completion, e:\Softwares\msys64\mingw64\bin\
add to the system environment. Note: The installation e:\Softwares
directory is adjusted according to the actual situation.
Install VSCode
This time, we chose VsCode as the tool, which is convenient and practical. Although the C++ plug-in is a bit weak, the one developed by Microsoft will not be too bad. After the installation is complete, select c/c++
Plugins . See below.
Open the software, then in the File
menu, click Add Folder to Workspace...
, select a directory as the folder for writing c++ code later, here we assume that the selected folder is cpp
. Create a new directory under cpp , and .vscode
then create three files .vscode
under the , directory, respectively . See below.task.json
c_cpp_properties.json
launch.json
tasks.cpp
The content is as follows:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"command": "g++",
"args": [
"-g",
" ${file}", //指定编译源代码文件
"-o",
" ${fileDirname}/${fileBasenameNoExtension}.exe", // 指定输出文件名,不加该参数则默认输出a.exe
"-ggdb3", // 生成和调试有关的信息
"-Wall", // 开启额外警告
"-static-libgcc", // 静态链接
"-std=c++17", // 使用最新的c++17标准
"-Wno-format",
"-fexec-charset=GBK", //Console窗体输出字符编码 保证能正常显示中文
"-finput-charset=UTF-8" //输入编译器文本编码 默认为UTF-8
],
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always", // 在“终端”中显示编译信息的策略,可以为always,silent,never
"focus": false,
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"\\"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
c_cpp_properties.json
Content:
{
"configurations": [
{
"name": "Win32",
"intelliSenseMode": "clang-x64",
"includePath": [
"${workspaceFolder}",
"e:/Softwares/msys64/mingw64/include/c++/7.3.0/"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=7",
"__cdecl=__attribute__((__cdecl__))"
],
"browse": {
"path": [
"${workspaceFolder}",
"e:/Softwares/msys64/mingw64/include/c++/7.3.0/"
]
},
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 3
}
launch.json
For code debugging, it doesn't work yet.
run the code
After completing the above work, in the cpp directory, write a test.cpp file, and then press Ctrl+Shift+B
, the test.exe file will be generated.