2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
First we need to prepare the C++ environment. Of course, if you think your current C++ environment is well configured and the C++ version can meet your daily usage needs, you can use the following two commands to query the C++ version:
g++ -v
g++ --version
You can simply judge whether the returned version can meet your needs. If you think the current version can support your future use, please jump directly to the second stage to configure the JSON file.
If you find that you have not configured your C++ properly, or you feel that the current version cannot support daily learning, then you need to download aMingw configures the C++ environment
I am currently using my latest Mingw as a demonstration
After downloading, unzip it to a folder you know, open the unzipped file path, and find a bin file
After opening it, copy the file path above. It is best to use the full English path when unzipping.
e.g. : D:ProgramFilesmingw64bin
Environment variables...
Edit the content in the Path below, Edit -> New, put the path on it, select this path and move it to the top.
Keep clicking OK to complete.
Test the g++ version.
Tools -> Build System -----> New Build System..., a *.sublime-build file will be generated, in which you can configure the C++ JSON file. Of course, you can also modify the existing build file.
Copy the following code into the file mentioned above, save it, and then select the JSON file you just edited again to compile C++.
{
"shell_cmd": "g++ -finput-charset=UTF-8 -fexec-charset=GBK -Wall "${file_name}" -o "${file_base_name}" && start cmd /c ""${file_base_name}" & pause"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++",
"encoding": "gbk",
}
by default,g++
Compilers usually do not use the latest C++ standard, but instead use a stable default standard (usually an older standard such as C++14 or C++17) to ensure compatibility with old code. Therefore, if you do not explicitly specify-std=c++20
or-std=c++23
, the compiler may compile using an older standard, which can cause errors when using features from the newer standard.
Therefore, if we want to use the latest compilation method, we must refer to the above, the maximum supported C++ version of our compiler, and update our JSON file to compile with the latest version of C++.
{
"shell_cmd": "g++ -std=c++23 -finput-charset=UTF-8 -fexec-charset=GBK -Wall "${file_name}" -o "${file_base_name}" && start cmd /c ""${file_base_name}" & pause"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c++",
"encoding": "gbk",
}
The specific change is only one line of code
{
"shell_cmd": "g++ -std=c++23 -finput-charset=UTF-8 -fexec-charset=GBK -Wall "${file_name}" -o "${file_base_name}" && start cmd /c ""${file_base_name}" & pause"",
//编译命令,使用C++20标准,指定输入和输出编码为UTF-8和GBK,并启用所有警告。如果编译成功,则启动一个新的命令行窗口运行生成的可执行文件,并在运行结束后暂停窗口。
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", // wrong answer 正则表达式匹配文件名等信息。
"working_dir": "${file_path}", // working_dir 编译的工作区
"selector": "source.c++", // 对象
"encoding": "gbk", // encoding 编码 : 选择 gbk(国标:包含所有的汉字)
}
{
[
{
"name": "Single File Build", // 只编译
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall "${file_name}" -o "${file_base_name}""
},
{
"name": "Single File Run", //只运行
"shell_cmd": "start cmd /c ""${file_base_name}" & pause""
},
{
"name": "Single File Build & Run", // 编译加运行
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall "${file_name}" -o "${file_base_name}" && start cmd /c ""${file_base_name}" & pause""
},
// 下面为多文件编译 , 请勿使用 。
{
"name": "Multiple Files Build",
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall *.cpp -o "${file_base_name}""
},
{
"name": "Multiple Files Run",
"shell_cmd": "start cmd /c ""${file_base_name}" & pause""
},
{
"name": "Multiple Files Build & Run",
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall *.cpp -o "${file_base_name}" && start cmd /c ""${file_base_name}" & pause""
}
]
}
{
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall "${file_name}" -o "${file_base_name}" && start cmd /c ""${file_base_name}" & pause"",
//编译命令,使用C++20标准,指定输入和输出编码为UTF-8和GBK,并启用所有警告。如果编译成功,则启动一个新的命令行窗口运行生成的可执行文件,并在运行结束后暂停窗口。
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", // wrong answer 正则表达式匹配文件名等信息。
"working_dir": "${file_path}", // working_dir 编译的工作区
"selector": "source.c++", // 对象
"encoding": "gbk", // encoding 编码 : 选择 gbk(国标:包含所有的汉字)
"variants":
[
{
"name": "Single File Build", // 只编译
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall "${file_name}" -o "${file_base_name}""
},
{
"name": "Single File Run", //只运行
"shell_cmd": "start cmd /c ""${file_base_name}" & pause""
},
{
"name": "Single File Build & Run", // 编译加运行
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall "${file_name}" -o "${file_base_name}" && start cmd /c ""${file_base_name}" & pause""
},
// 下面为多文件编译 , 请勿使用 。
{
"name": "Multiple Files Build",
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall *.cpp -o "${file_base_name}""
},
{
"name": "Multiple Files Run",
"shell_cmd": "start cmd /c ""${file_base_name}" & pause""
},
{
"name": "Multiple Files Build & Run",
"shell_cmd": "g++ -std=c++20 -finput-charset=UTF-8 -fexec-charset=GBK -Wall *.cpp -o "${file_base_name}" && start cmd /c ""${file_base_name}" & pause""
}
]
}