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
- Resolved the issue where Sublime Text outputs the system PATH when encountering compilation errors (finally found the root cause).
- Configured Clang++ and g++ to compile simultaneously, and suppressed g++’s prompt messages when compilation fails (using
1>nul
). - Configured gprof to analyze code (which is also the reason for using both clang and g++ to compile simultaneously).
- 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.
- After installing Clang (LLVM), add it to the system environment variable (PATH).
- Install WinGW.
- Add
\MinGW\bin
and\MinGW\x86_64-w64-mingw32\include
to the system environment variable (PATH). - After logging off/restarting, try:
clang -v
andgcc/g++ -v
, as shown in the images below.
Now we can give it a try.
Let’s start with some code:
1 | #include <iostream> |
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:
- Locate the Default.sublime-package under the Sublime Text 3 installation path, e.g., ~Sublime Text 3\Packages\Default.sublime-package.
- Extract the Default.sublime-package file (just change the extension to zip or rar to extract) and obtain the exec.py file.
- Place the exec.py file in the path C:\Users{UserName}\AppData\Roaming\Sublime Text 3\Packages.
- Comment out the line
self.append_string(proc, self.debug_text)
in exec.py. - 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:
- When Ctrl+B is pressed, the build system should cd to the source file’s directory.
- In this directory, use clang to compile the source file.
- 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 | { |
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 | { |
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