📜 ⬆️ ⬇️

Sublime Text 2 for C / C ++ Developer or SublimeClang

On Habré many articles devoted to the editor Sublime Text 2. Usually they give rise to a lot of comments, where supporters of ST2 and various other editors (as well as full IDE) indulge in a fun process of "dignity of the metrics."
However, among the comments, I personally have never once mentioned references to the SublimeClang plugin, which radically changes the work of a C ++ programmer in this editor. Search on Habra issued only 4 mentions of a plug-in in comments.
I do not know what caused such a disregard. Good things are not discussed? ST2 do not use C and C ++ developers? I want to correct the situation with my article. I will talk about some, perhaps not obvious, features of installation and configuration, as well as share a couple of my own techniques and scripts.

About plugin


So, the plugin provides code completion and error checking right while writing a program. For C / C ++ / ObjC / ObjC ++ files, the plugin replaces the standard autocompletion of the editor with its own. To do this, the plugin in the background runs the source through the clang compiler, which forms the AST. The plugin uses the received information for intelligent prompts of autocompletion.
The closest analogue I can pick up is the IntelliSense function in MS Visual Studio.
Using the information first-hand - from the compiler - allows the plugin to correctly find out which variables, functions, types, etc. available at this particular program point. Also, Go to definition (alt + d, alt + d) and Go to implementation (alt + d, alt + i) work Go to definition (alt + d, alt + d) some extent. The list of other hotkeys is available on the project page.


Installation


Windows and Mac OS X users are lucky - the plug-in is installed elementarily, through the Sublime Package Manager.
Linux users are less fortunate. For Linux, the necessary libraries are not supplied assembled, so you need to install the plugin from the git repository and build it manually. (In general, you can also install via the Package Manager, only it removes all manually assembled libraries every time it decides to check for updates). Assembly instructions are on the plugin page, the “Additional Prerequisites (Linux Only)” item.
')
Installation example on my Archlinux x64 system
  1. Install clang
  2. Install python26
  3. To defeat the “ctypes can't be imported” error, make symlink:
     ln -s /usr/lib/python2.6/ /opt/sublime_text/lib/python2.6 

  4. Install SublimeClang
     git clone --recursive git://github.com/quarnster/SublimeClang.git 

  5. Since in archlinux libclang.so is located in / usr / lib / llvm /, i.e. not where SublimeClang expects to see it, I do symlink inside the plugin folder.
     ln -s /usr/lib/llvm/libclang.so 

  6. I collect the missing library plugin for instructions from the project page.
     cd src mkdir build cd build cmake .. make 



Customization


Plugin is configured in a standard way for ST2.
There is a file SublimeClang/SublimeClang.sublime-settings . The file is well documented, all possible settings are listed there and in the comments there are examples of use.
There is User/SublimeClang.sublime-settings , where you should transfer your own settings.
It is also possible to specify project-specific parameters. They are specified in the project file ( Project - Edit project ) in the "Settings" section. The prefix "sublimeclang" added to the name of each plugin option. Thus, for example, settings for a project using C ++ 11 might look like

 "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 make in the User-settings is "debug_options": true . This option includes a list of parameters that are passed to the compiler. After switching on, the compiler parameters will be displayed in the editor console ( View - Show console ).

Integration with build scripts


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 doesn't know anything about the standard).
The main way to solve the problem is to use the plugin "options" and "additional_language_options" , which allow you to pass the path and necessary defaults to the compiler. Naturally, you can specify general and project-specific settings. for example

 "sublimeclang_options": [ "-I~/libs/boost_1_52_0", "-I~/code/sdl2/root/include" ] 


The method is universal and simple as a boot, but 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 the option “options_script” in which you can specify the command that will be invoked for each file. This command should return a list of compiler options for this particular file. In this way, you can write your own scripts that will ask the compiler settings of the build system and communicate them to the plugin.

The developer of the plugin in the comments to the option gives a link to the https://gist.github.com/3944250 script, which takes the compilation settings from CMake.
For my current project, I slightly edited the script. Now
  1. the script selects the settings for the header files;
  2. The folder in which CMake is building is not hard coded. Instead, the script searches for the build folder, passing recursively to the top of the folder tree. I have a lot of different small CMake projects in one ST2 project.

My version of the script 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 specify ST2 to read .ino files as C ++ files. To do this, in the lower right corner, click on the choice of the code highlighting code and choose C ++ in the submenu "Open all with current extension as..." .

Theoretically, you can create a script that pulls the compilation commands from the Makefile scripts. GNU make allows you to dump all variables and rules ( make -np ), which you can then parse and pull out the compile commands. And I even began to write a script, but abandoned the case.

disadvantages


Well, where do without them.

  1. In some confused cases (addition of the template parameters of some confused template class), the plugin drops and drops ST2. It happens rarely, I have seen this only a couple of times during my experiments with new C ++ 11 features. It is unpleasant, of course, but it is saved by the fact that ST2 very often makes autosave. I lost only a couple of recent code changes.
  2. Sometimes the plugin does not see options to add. Especially if there are errors in the file. Which, of course, is logical; one cannot rely on the source code with errors, but nevertheless, it is not pleasant.
  3. A plugin sometimes does not suggest 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 start writing method_name - does not tell. It does not always repeat, I can’t say more specifically when it happens - I just didn’t research it.
  4. The disadvantage is probably not the plug-in itself, but my script for extracting compiler parameters from the cmake database. Suppose we go to the definition of 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 gives a lot of errors for 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 to quickly check their ideas or to run small projects for which it will not be suitable to uncover a full-fledged IDE. ST2, with its rich text editing and navigation capabilities, along with the SublimeClang plugin, becomes an excellent developer tool.

Source: https://habr.com/ru/post/167353/


All Articles