Configure SublimeText as a lightweight IDE for C/C++

配置SublimeText为C/C++的轻量级IDE

As a C++ programmer, I have long been accustomed to using tools like GCC, but I must say that GCC’s error messages are terrible. Although there are error prompts, it often requires manual debugging to understand them… Now, the good news is here, the accuracy of Clang‘s messages is incomparably better than that of GCC.

However, Clang relies on MinGW as its backend when compiling C/C++ programs, so both need to exist, which is just fine by me.

2016.04.09-Upgrade

  1. Resolved the issue where Sublime Text outputs the system PATH when encountering compilation errors (finally found the root cause).
  2. Configured Clang++ and g++ to compile simultaneously, and suppressed g++’s prompt messages when compilation fails (using 1>nul).
  3. Configured gprof to analyze code (which is also the reason for using both clang and g++ to compile simultaneously).
  4. Used SublimeClang for coding hints and error notifications.

Mingw/+Clang Environment Configuration

First, you can download Clang (LLVM-3.7.0-win64) and MinGW (x86_64-5.2.0-POSIX-SEH-RT_V4-REV1) here.

  1. After installing Clang (LLVM), add it to the system environment variable (PATH).
  2. Install WinGW.
  3. Add \MinGW\bin and \MinGW\x86_64-w64-mingw32\include to the system environment variable (PATH).
  4. After logging off/restarting, try: clang -v and gcc/g++ -v, as shown in the images below.


Now we can give it a try.
Let’s start with some code:

1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main(void)
{
cout<<"HelloWorld"<<endl;
return 0;
}

Horizontal Comparison of Clang and GCC

First, let’s try using Clang++:

Then, let’s deliberately delete a semicolon and check, and to compare, I’ll also compile it with g++:

Well, I declare G++ completely defeated.

Next, let’s try a slightly larger piece of code:
Clang:

G++:

Ah, let me calm down; this is truly an unforgettable experience.

Now we move on to the second part:

Configuring Sublime Text to Use Clang for Code Compilation

Previously, I always used Sublime’s default build system to compile code, but after upgrading to Sublime Text 3, many strange issues arose, for instance, the most unbearable below (Resolved):

Solution:

  1. Locate the Default.sublime-package under the Sublime Text 3 installation path, e.g., ~Sublime Text 3\Packages\Default.sublime-package.
  2. Extract the Default.sublime-package file (just change the extension to zip or rar to extract) and obtain the exec.py file.
  3. Place the exec.py file in the path C:\Users{UserName}\AppData\Roaming\Sublime Text 3\Packages.
  4. Comment out the line self.append_string(proc, self.debug_text) in exec.py.
  5. Restart Sublime Text, and the issue is resolved.


When a compilation error occurs, it traverses the system PATH, and after rewriting the build rules multiple times without results, I chose to endure. However, enduring is not my style, so while messing around with Clang today, I decided to tackle this issue.

Actually, the default build system for C/C++ in Sublime Text 3 is also incredibly weak; the program output gets captured in Sublime Text’s window, making it impossible to run and input information. If scanf/cin is encountered, it crashes. So, most solutions involve using the default compilation system to compile the code and capture error messages, running the executable file via cmd if the compilation succeeds.

Thus, the ball is back in my court; I don’t want errors (which are actually the PATH information) to display in the Sublime Text window. I want to use a cmd window to show compilation/errors and another to run the program.

Having conceived this idea, I did some Googling with the mindset of not reinventing the wheel, but it seems nobody mentioned this issue, so I’ll have to create a new solution myself.

I found a document about the Sublime Text build system here, and apart from basic syntax, build system variables are essential:

Build System Variable Meaning
$file_path Current file directory, e.g., C:Files.
$file Full path of the current file, e.g., C:FilesChapter1.txt.
$file_name Full filename of the current file, e.g., Chapter1.txt.
$file_extension Extension of the current file, e.g., txt.
$file_base_name Name of the current file, e.g., Document.
$packages Full path of the Packages directory.
$project Full path of the current project file.
$project_path Full directory of the current project file.
$project_name Full filename of the project file.
$project_extension Extension of the current project file.
$project_base_name Basic filename of the current project file.

For more information, click here——Sublime Text Build System.

Now that we have Sublime’s build system variable names and basic syntax, to achieve one-click launching of two terminals for compilation/running, we need to solve the following issues:

  1. When Ctrl+B is pressed, the build system should cd to the source file’s directory.
  2. In this directory, use clang to compile the source file.
  3. After compilation, launch the program (it must compile successfully first, as there is no executable to run if compilation fails).

Actually, the first two steps are not too difficult; we can use & to connect the two statements.
The manual command looks like this:
cd C:\Users\visionsmile\Desktop & clang++ -o test.exe test.cc -std=c++11 -w
So, I said that build system variables are the most important.

Using build system variables, we can write it like this:
cd $file_path & clang++ -o $file_base_name.exe $file_name -std=c++11 -w
Then, we can organize it according to the syntax (launch a new window to compile the code when Ctrl+B is pressed):
"cmd": ["start", "cmd", "/k", "cd", "$file_path", "&", "clang++", "-o", "${file_base_name}.exe", "${file_name}", "-std=c++11"]
There were a lot of issues along the way, but I will skip them. The idea is like this.

Let’s talk about the problem I encountered: I didn’t realize how to use the build system’s syntax to check whether the compilation was successful. So, if compilation and running are written together and compilation encounters an error, the executing window would also still execute. However, since the executable file doesn’t exist due to the compilation failure, an error will be thrown, and my perfectionism can’t tolerate that, so I chose a compromise solution:

By making a choice, I can execute either plan; the first is compile, and the second is run the program.

Here’s the complete configuration (Updated):

Build——uses Clang++ and g++ to compile simultaneously.
Build - RUN——runs the version compiled by Clang++.
Build - G++——runs the version compiled by G++.
Build - G++ Profiler——analyzes the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"cmd": ["clang++", "-o", "${file_path}/${file_base_name}_clangxx.exe", "${file}", "-std=c++11", "-w","&","g++", "-o", "${file_path}/${file_base_name}_gxx.exe","${file}", "-std=c++11", "-w", "-pg","2>nul"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"shell": true,
"variants":
[
{
"name": "Run",
"cmd": ["start","console_runner", "${file_path}/${file_base_name}_clangxx.exe"]
},
{
"name": "G++ Run",
"cmd": ["start","console_runner", "${file_path}/${file_base_name}_gxx.exe"]
},
{
"name": "G++ Profiler",
"cmd": ["gprof","-b", "${file_base_name}_gxx.exe","gmon.out",">","${file_base_name}_prof.txt", "&", "C:\\Program Files\\SystemTools\\Sublime Text 3\\sublime_text.exe", "${file_base_name}_prof.txt"]
}
]
}

Test Screenshots:
Compilation with errors:

Fixing the errors leads to successful compilation and running:

Using SublimeClang for Code Suggestions

There are many tutorials online, but many are based on the 4.X version of Win32’s MinGW. The version of Mingw (gcc version 5.2.0 (x86_64-posix-seh-rev1)) I use has a library path different from 4.X. Below, I’ll directly throw up my configuration file.
Note: The SublimeClang linked must be used with 32-bit Sublime Text; 64-bit cannot be used.

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
40
41
42
43
44
{
"show_output_panel": false,
"dont_prepend_clang_includes": true,
"inhibit_sublime_completions": false,
"hide_output_when_empty": true,
"show_visual_error_marks": true,
"options":
[
"-m32",
//"-w",
"-ferror-limit=9",
"-fgnu-runtime",
"-fms-extensions",
"-nostdinc",
// "-std=c++11",
"-std=gnu++11",
// "-std=g++11",
// Fill in the actual location and version number for your PC here.
"-isystem", "C:\\Program Files\\CompileBuild\\MinGW\\x86_64-w64-mingw32\\include",
"-isystem", "C:\\Program Files\\CompileBuild\\MinGW\\x86_64-w64-mingw32\\include\\c++",
"-isystem", "C:\\Program Files\\CompileBuild\\MinGW\\lib\\gcc\\x86_64-w64-mingw32\\5.2.0\\include",
"-isystem", "C:\\Program Files\\CompileBuild\\MinGW\\x86_64-w64-mingw32\\include\\c++\\x86_64-w64-mingw32",
"-isystem", "C:\\Program Files\\CompileBuild\\MinGW\\include",
"-isystem", "C:\\Program Files\\CompileBuild\\MinGW\\lib\\gcc\\x86_64-w64-mingw32\\5.2.0\\include",
"-D__GNUC__=4",
"-D__GNUC_MINOR__=2",
"-D__GNUC_PATCHLEVEL__=1",
"-D__GXX_ABI_VERSION__=1002",
"-Di386=1",
"-D__i386=1",
"-D__i386__=1",
"-DWIN32=1",
"-D_WIN32=1",
"-D__WIN32=1",
"-D__WIN32__=1",
"-DWINNT=1",
"-D__WINNT=1",
"-D__WINNT__=1",
"-D_X86_=1",
"-D__MSVCRT__=1",
"-D__MINGW32__=1",
"-D__STDC_VERSION__=201112L"
],
}

Postscript
Actually, this doesn’t have much technical content. I write this down mainly to remind myself to read more documentation. Reading documentation is much better than aimlessly Googling or burying myself in building a wheel.

My configuration file can be downloaded here: SublimeText

The article is finished. If you have any questions, please comment and communicate.

Scan the QR code on WeChat and follow me.

Title:Configure SublimeText as a lightweight IDE for C/C++
Author:LIPENGZHA
Publish Date:2015/12/26 21:54
Update Date:2017/04/10 18:18
Word Count:6.2k Words
Link:https://en.imzlp.com/posts/14596/
License: CC BY-NC-SA 4.0
Reprinting of the full article is prohibited.
Your donation will encourage me to keep creating!