Sublime Text 2 for a C / C ++ developer or SublimeClang
There are a lot of articles on the hub devoted to the editor Sublime Text 2. Usually they give rise to a lot of comments, where the supporters of ST2 and all kinds of other editors (as well as full-fledged IDEs) indulge in a fun process of "worthiness".
However, among the comments I personally have never met references to the SublimeClang plugin , which dramatically changes the work of a C ++ programmer in this editor. Search in Habr returned only 4 mentions of the plugin in the comments.
I don’t know what caused this ignoring. Good things are not discussed? ST2 do not use C and C ++ developers? With my article I want to rectify the situation. I will talk about some, perhaps not obvious, features of installation and configuration, as well as share a couple of my own tricks and scripts.
So, the plugin provides code completion and error checking right in the process of writing the program. For C / C ++ / ObjC / ObjC ++ files, the plugin replaces the standard editor autocompletion with its own. To do this, the plugin in the background runs the source through the clang compiler, which forms the AST. The plug-in uses the received information for intelligent auto-completion tips.
The closest analogue that I can pick up is the IntelliSense function in MS Visual Studio.
Using first-hand information - from the compiler - allows the plugin to correctly recognize which variables, functions, types, etc. available at this particular point in the program. In the same way,
Windows and Mac OS X users are lucky - installing the plugin is elementary, through the Sublime Package Manager.
Linux users - less fortunate. For Linux, the required libraries are not supplied assembled, so you need to install the plugin from the git repository and build manually. (In general, it is possible to install something through the Package Manager, only it deletes all the libraries compiled manually each time it decides to check for updates). Assembly instructions are on the plugin page, item "Additional Prerequisites (Linux Only)".
The plugin is configured in the standard way for ST2.
There is a file
There
It is also possible to specify project-specific parameters. They are indicated in the project file (
Standard general settings are quite adequate. The only thing that I change and enter into the User settings is this
The main problem I encountered is that the plugin (and therefore the compiler) does not know where to look for the header files of non-standard libraries. (On Windows, he probably knows nothing about standard ones).
The main way to solve the problem is to use the plugin options
The method is universal and simple as felt boots, but it contains a significant drawback - the settings, in fact, repeat the information that is already known to the build system that collects the project code.
The plugin provides an option "options_script", in which you can specify the command that will be called for each file. This command should return a list of compiler options for this particular file. Thus, you can write your own scripts that ask the compiler for the build system and tell them to the plugin.
The plugin developer in the comments on the option gives a link to the script https://gist.github.com/3944250 , which takes the compilation settings from CMake.
For my current project, I slightly edited the script. Now
My version of the script is https://gist.github.com/4641812
Similarly, I integrated with the ino utility, which is used to build arduino projects. Link to the script https://gist.github.com/4641886 . The script header contains the settings that must be added to the ST2 project file. You should also tell ST2 to read files as
Theoretically, you could create a script that pulls compilation commands from Makefile scripts. GNU make allows you to dump all variables and rules (
Well, and where without them.
In this article I tried to describe the subtleties of the settings, which, in my opinion, are not well described in the documentation. I hope this article will help those developers who, like me, would like to have a tool for quickly checking their ideas or for conducting small projects for which it is not good to uncover a full-fledged IDE. ST2, with its rich text editing and navigation capabilities, along with the SublimeClang plugin, is becoming a great tool for the developer.
However, among the comments I personally have never met references to the SublimeClang plugin , which dramatically changes the work of a C ++ programmer in this editor. Search in Habr returned only 4 mentions of the plugin in the comments.
I don’t know what caused this ignoring. Good things are not discussed? ST2 do not use C and C ++ developers? With my article I want to rectify the situation. I will talk about some, perhaps not obvious, features of installation and configuration, as well as share a couple of my own tricks and scripts.
About the plugin
So, the plugin provides code completion and error checking right in the process of writing the program. For C / C ++ / ObjC / ObjC ++ files, the plugin replaces the standard editor autocompletion with its own. To do this, the plugin in the background runs the source through the clang compiler, which forms the AST. The plug-in uses the received information for intelligent auto-completion tips.
The closest analogue that I can pick up is the IntelliSense function in MS Visual Studio.
Using first-hand information - from the compiler - allows the plugin to correctly recognize which variables, functions, types, etc. available at this particular point in the program. In the same way,
Go to definition (alt + d, alt + d)
and work to some extent Go to implementation (alt + d, alt + i)
. The list of other hot keys is available on the project page.Installation
Windows and Mac OS X users are lucky - installing the plugin is elementary, through the Sublime Package Manager.
Linux users - less fortunate. For Linux, the required libraries are not supplied assembled, so you need to install the plugin from the git repository and build manually. (In general, it is possible to install something through the Package Manager, only it deletes all the libraries compiled manually each time it decides to check for updates). Assembly instructions are on the plugin page, item "Additional Prerequisites (Linux Only)".
Installation example on my Archlinux x64 system
- Install clang
- Install python26
- To defeat the error “ctypes can't be imported” make symlink:
ln -s /usr/lib/python2.6/ /opt/sublime_text/lib/python2.6
- Install SublimeClang
git clone --recursive git://github.com/quarnster/SublimeClang.git
- Since in archlinux libclang.so is located in / usr / lib / llvm /, i.e. not where SublimeClang expects to see it, I make symlink inside the plugin folder.
ln -s /usr/lib/llvm/libclang.so
- I collect the missing libraries for the plugin according to the instructions from the project page.
cd src mkdir build cd build cmake .. make
Customization
The plugin is configured in the standard way for ST2.
There is a file
SublimeClang/SublimeClang.sublime-settings
. The file is well documented, all possible settings are listed there, and there are usage examples in the comments. There
User/SublimeClang.sublime-settings
is where you should transfer your own settings. It is also possible to specify project-specific parameters. They are indicated in the project file (
Project - Edit project
) in the section "Settings"
. A prefix is added to the name of each plugin option "sublimeclang"
. Thus, for example, the settings of a project using C ++ 11 may look like this"settings":
{
// Настройки самого редактора
"trim_trailing_white_space_on_save": true,
// Настройки плагниа
// В основном файле настроек плагина эта опция называется "additional_language_options". Здесь же добавляем префикс.
"sublimeclang_additional_language_options":
{
"c++" :["-std=c++11",
"-stdlib=libc++"
]
}
}
Standard general settings are quite adequate. The only thing that I change and enter into the User settings is this
"debug_options": true
. This option enables the output of a list of parameters that are passed to the compiler. After switching on, the compiler options will be displayed in the editor console ( View - Show console
).Build Script Integration
The main problem I encountered is that the plugin (and therefore the compiler) does not know where to look for the header files of non-standard libraries. (On Windows, he probably knows nothing about standard ones).
The main way to solve the problem is to use the plugin options
"options"
and "additional_language_options"
, which allow you to pass the path and necessary defines to the compiler. Naturally, you can specify general and project-specific settings. for instance"sublimeclang_options":
[
"-I~/libs/boost_1_52_0",
"-I~/code/sdl2/root/include"
]
The method is universal and simple as felt boots, but it contains a significant drawback - the settings, in fact, repeat the information that is already known to the build system that collects the project code.
The plugin provides an option "options_script", in which you can specify the command that will be called for each file. This command should return a list of compiler options for this particular file. Thus, you can write your own scripts that ask the compiler for the build system and tell them to the plugin.
The plugin developer in the comments on the option gives a link to the script https://gist.github.com/3944250 , which takes the compilation settings from CMake.
For my current project, I slightly edited the script. Now
- the script selects the settings for the header files;
- The folder where CMake builds is not hard-coded. Instead, the script searches for the build folder, going recursively to the top of the folder tree. I have many different small CMake projects in one ST2 project.
My version of the script is https://gist.github.com/4641812
Similarly, I integrated with the ino utility, which is used to build arduino projects. Link to the script https://gist.github.com/4641886 . The script header contains the settings that must be added to the ST2 project file. You should also tell ST2 to read files as
.ino
C ++ files. To do this, in the lower right corner, click on the selection of the code highlighting scheme and select C ++ in the submenu "Open all with current extension as..."
. Theoretically, you could create a script that pulls compilation commands from Makefile scripts. GNU make allows you to dump all variables and rules (
make -np
), which you can then parse and pull out compilation commands. And I even started to write a script, but abandoned this thing.disadvantages
Well, and where without them.
- In some confused cases (addition of the template parameters of some confused template class), the plugin crashes and drops ST2. It happens rarely, I only saw this a couple of times during my experiments with new C ++ 11 features. It is unpleasant, of course, but it saves that ST2 very often does autosaves. I only lost a couple of recent code changes.
- Sometimes the plugin does not see options for additions. Especially if there are errors in the file. Which, of course, is logical, it is impossible to rely on the source with errors, but nevertheless, it is not pleasant.
- A plugin sometimes does not prompt the name of a class method when you write its call in the body of another method of the same class. this-> method_name - prompts. Just starting to write method_name doesn’t tell you. It is not always repeated, I can’t say more specifically when this happens - I just haven’t investigated it.
- The drawback is probably not the plugin itself, but my script for extracting compiler parameters from the cmake database. Suppose we move on to defining a function (or class, whatever) in the header file of a third-party library. The compiler settings for this header file are no longer known to the plugin and it is lost, it produces a bunch of errors on unknown types, etc.
Conclusion
In this article I tried to describe the subtleties of the settings, which, in my opinion, are not well described in the documentation. I hope this article will help those developers who, like me, would like to have a tool for quickly checking their ideas or for conducting small projects for which it is not good to uncover a full-fledged IDE. ST2, with its rich text editing and navigation capabilities, along with the SublimeClang plugin, is becoming a great tool for the developer.