Technology Sharing

Configure the C compiler in sublime (.sublime-build) to use C20 in sublime, a beginner's tutorial

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Preliminary preparation

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
  • 1
g++ --version
  • 1

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.

  • GCC 4.8: Support C++11 (partially)
  • GCC 4.9: Support for C++11 and C++14 (partially)
  • GCC 5: Full support for C++14
  • GCC 6: Support for C++14 and C++17 (partially)
  • GCC 7: Support C++17 (mostly)
  • GCC 8: Full support for C++17, partial support for C++20
  • GCC 9: Support more C++20 features
  • GCC 10: Supports most C++20 features
  • GCC 11: More comprehensive support for C++20
  • GCC 12: Supports almost all C++20 features
  • GCC 13 and later: Added full support for C++20 and started support for C++23

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

![[Pasted image 20240711232026.png]]

After downloading, unzip it to a folder you know, open the unzipped file path, and find a bin file

[[Pasted image 20240711232411.png]]

After opening it, copy the file path above. It is best to use the full English path when unzipping.

e.g. : D:ProgramFilesmingw64bin
  • 1
  1. Editing system environment variables

Pasted image 20240711232650

  1. Environment variables...
    ![[Pasted image 20240711232725.png]]

  2. Edit the content in the Path below, Edit -> New, put the path on it, select this path and move it to the top.
    ![[Pasted image 20240711232813.png]]

  3. Keep clicking OK to complete.

Test the g++ version.

2. Environment Configuration

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.

1. Fool-proof configuration method

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",  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2. Basic configuration method

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",  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

The specific change is only one line of code

![[Pasted image 20240711234819.png]]

3. In-depth analysis

Basic configuration items

{  
   "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(国标:包含所有的汉字) 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
Constructing variants of text
{
	[  
       {  
           "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""  
       }  
   ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
total
{  
   "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""  
       }  
   ]  

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39