Showing posts with label clang. Show all posts
Showing posts with label clang. Show all posts

2013-12-30

C++ Hello World Using clang_complete and Vim

Many posts on the advantages of clang_complete have already been posted. For example, there's a demonstration of the plugin on Mac. If you can understand Chinese (or you have a translator that truly understands it), you may even look at more sites, such as Chen's post.
The authors of the above posts are likely to work in the IT sector. For new comers of programming, they may like to start from zero. On which platform can they start from? I'd recommend popular *nix distributions (e.g. Debian-based/Fedora-based ones) due to their package managing tools like APT or YUM. For a M$ Vis* Stu* like code completion on Vim, clang_complete is the right answer, but it is often hard to install on M$ Win* [1], but the installation is incredibly easy on *nix. [2]
Many writers of posts on clang_complete installation stopped writing further after completing the whole installation and configuration of the plugin, and I haven't seen any post that uses "hello world" as an example. This is alright since many users of Vim are experienced programmers who would like to demonstrate some advanced features of the editor with the additional plugin (e.g. jumping between ctags), and a simple hello.cpp can't be used for that. Moreover, many teachers would teach programming on M$ Win*, and even if a *nix desktop is used, the work is most likely done using an IDE on GUI (Emacs is a text editor and IDE that runs on command line.) ... The concept of modal editing may be too hard for newbies to accept it, and you need to get several commands right before entering any text into a buffer. The steep learning curve might frighten learners away from Vim... Some computer users believe in the myth that Vim and Emacs are too old and nobody use them.
Anyways, the mixed use of both the built-in omni-complete function of Vim (invoked by <C-n> or <C-p>) and the popup menu of clang_complete (invoked by <C-x><C-u>)to write a "hello world" program is a productive way of doing the job. However, due to the limitations of human mind, we can remember very few unfamiliar things at a time. This is not the case in storage devices. They can capture every word that come out of our mind, though many physical means of storage of imformation are subject to aging, especially those in the past... Luckily, information technology allows words, and even fonts, to be replicated efficiently and almost identically. This has brought the human race a variety of new subjects, such as web *ng, and has extended our old knowledge into many fields, such as typography... In addition, the advancement of technology enables us to capture visible images and copy it rapidly, and I've recorded my experience of "hello world" using clang_complete with Vim on a series of screenshots.
In addition to adding Wu's custom vimrc configuration for clang_complete [3], I've also installed vim-snipmate and vim-snippets for more auto-complete mappings.
Before actually beginning the steps for writing a "hello world", we should know that we won't learn anything from successes. It's failures that help us reflect on the mistakes in our understand of theories. Therefore, I'll deliberately find something wrong, and elaborate on that using some ideas. In other words, that's "to blow water" in Cantonese.

1. Comparison between clang_complete and the editor's built-in completion in typing #include<iostream>

1.1 The editor's built-in completion

The following "hello world" procedure contains some wrong steps.


  1. Type inc<Tab>.
  2. Vim-snippet completes the code, with stdio.h as default. The file name of the included file is selected so that you can input something else.
  3. Since cout is used later, we need to #include<iostream>. First type io to override the default input.
  4. Type <C-n> or <C-p> to get a completion list.
  5. Select iostream (without 's').
  6. Continue typing the whole program, and compile it.
  7. The source code fails!
Obviously, there's a syntax error at the line #include<iostream.h>. Thinking about the auto-completion function again, one will notice that the correct step is to press <Esc> after step 1, and use ci< to clear the code inside the <> block, that's a good try, but I personally think that the steps can be more beautiful.

1.2 clang_complete's solution

This is just a part of correct steps. I leave the remaining parts of codes of this simple program into later sections.



This time, after typing # at the beginning of the line, press <C-x><C-u> to use the automatic completion list, and choose the # include option with the $`header' surrounded by a <> block, not a double quote. Use <Alt-Tab> to select the inner <> block and type io<C-n> to use the built-in (not plugin) function to complete the code. (The <Alt-Tab> keystroke doesn't work in GUI, since it means changing the window on focus.)
Note: In this method, stdio.h needs to be included. Otherwise, the built-in insert completion popup menu containing iostream will not be shown.

2. Completion of using namespace std;

  1. Without clang_complete: Directly type in everything, or use <C-n> for word-by-word completion.
  2. With clang_complete:  The completion menu can complete the whole line.
Analysis: Finding an item from a menu requires coordination of our eyes and hands, while typing the code directly requires more memory. For a newbie, the former should be easier since he/she doesn't waste time to recall his/her memory. For a geek, the later should be faster since the process has been repeated for many time. Luckily, being the second in the popup list, the problem of reaction time does not matter much if one looks at the list from the top to the bottom. Moreover, typing the code directly may lead to syntax errors. This problem can be overcome if one has the capability to read the error message from the compiler and to browse code quickly.

Without clang_complete: word-by-word completion
With clang_complete: completion of the whole line

The remaining parts of this post doesn't make use of the clang_complete plugin.

3. The main method

Just typing main<Tab> will do. That's from vim-snippets.


4. Greeting the world!

If you type cout<Tab>, you'll get std::cout<< <<std::endl;, with the cursor located in between the two whitespace characters in the middle. Using a regular expression to delete the std:: in front of cout and endl.


5. Getting the indentation correct

Modifying the c.snippet file maybe a bit troublesome, and may bring strange consequences to the plugin. If you don't have time to study the plugin, you may not like to bother it. Manually decreasing the indentation of the line return 0; each time is not likely to be an elegant solution for Vim Lovers. The actual way to fix the problem (using the command gg=G in the normal mode) is on p. 73 of the book Vi iMproved—Vim. [4]


6. Running the program

For some newbies, it might be too much for them to digest if I discuss more on the creation of a sample makefile, which automatically executes more complicated compiling commands with a simple make command, searching "makefile tutorial" on the web will do. If you don't like the clang compiler, just substitute clang++ with g++.
According to Henri Poincaré, a French mathematician, things are beautiful if they are simple. Repetition of facts build up our understanding. I end this post with a short and simple command that can be repeatedly used to compile .cpp source files with different file names. For more details about that, refer to my earlier post titled Fast Compilation and Execution of Source Code.



Reference:
[1]: http://blog.chinaunix.net/uid-53564-id-3558537.html
[2]: http://changyy.pixnet.net/blog/post/31706673-%5Bvim%5D-using-clang_complete-plugin-@-ubuntu-12.04
[3]: http://blog.wuwon.id.au/2011/10/vim-plugin-for-navigating-c-with.html
[4]: ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf

2013-12-12

OmniCppComplete Installation for Dummies

Note: In Stack Overflow question 12339415, it is suggested that clang is better. For its installation, refer to its another fan's blog.
Background: OmniCppComplete is a great Vim plugin that has the popup menu like many big IDEs. A Youtube video can show the power of OmniCppComplete.
Download: OmniCppComplete requires ctags.
  1. OmniCppComplete: GitHub Repository (To avoid mixing up of files from different plugins, use one plugin management plugin like Pathogen or Vundle.)
  2. Ctags: Official homepage on SourceForge (The .zip file for Win* 2**0 is fine.)
For more detailed information on the installation of ctags, please refer to my earlier blog post Links to Good Posts.
Problem: Reading the OmniCppComplete's documentation only, you won't be able to understand how to use this plugin, so follow some tutorials, and the most viewed one should be on Vim Tips Wiki. Followed its guidance, I got error in typing std:: in a new .cpp file. Omni completion told me something like "pattern not match". I spent nearly 2 hours to figure out what's wrong, and finally managed to fully understand the contents of this blog entry.
Now, I'm going to make a Win* version of Vim Tips Wiki's tutorial, using the ideas from Carl.
Solution:
  1. I suppose OmniCppComplete and ctags are installed properly.
    • OmniCppComplete does not mix up with other plugins.
      Note: For gVimPortable, do store files under the Data folder, but NOT the $VIMRUNTIME file. Otherwise the settings will be lost after upgrading gVimPortable.
    • The folder for different plugins are separated (except the plugin management plugin itself).
    • Typing :h omnicppcomplete in Vim normal mode gives up the help file for OmniCppComplete.
    • Typing :h omnicppcomplete gives you the corresponding help file.
    • The folder for ctags is in the PATH environment variable.
      Follow this page for setting the PATH variable.
      C:\ctags58 should be in the PATH variable.
  2. Download the modified libstdc++ headers from GitHub.
  3. Unzip the folder and place it under "somewhere meaningful to you"
    My suggestion: C:\Users\[your-user-name]\.vim
    Here you can substitute [your user name] with your real user name on Win*.
    Note: In the dialogue (in case you use GUI), DON'T input tags at the end. Otherwise, you'll get C:\Users\[your-user-name]\.vim\tags\tags
  4. If things go right, you should have C:\Users\[your-user-name]\.vim\tags
    Go to that directory by typing cd .vim\tags in Win*'s Command Prompt, which can be involked by typing cmd in the "Start" menu. (For XP, choose "Run" and type "cmd" in the popup dialogue. If you like choosing from the menu, follow the way on the official website.)
    At first, a commad prompt should be like this.
    The current directory is C:\Users\Owner.
    Change the current directory to C:\Users\Owner\.vim\tags.
    The contents should be shown by typing dir (There're so many files that I use the /w option to save space.)
  5. The contents are in the right place, so we may proceed.
    Get a list of ctags.
    To get a list of ctags, the correct command:
    >ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f cpp .
    The one you see on Vim Tips Wiki:
    >ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f cpp cpp_src
  6. Then you should have a huge list of ctags with the file name cpp. Not adding the additional tags for OpenGL, etc, is OK.
  7. Finally, adding the lines found in the Vim Tips Wiki into your vimrc will do.
    Note: If you don't add the additional tags in step 5, then the following lines should NOT be added.
    set tags+=~/.vim/tags/gl
    set tags+=~/.vim/tags/sdl
    set tags+=~/.vim/tags/qt4

Testing

A picture can illustrate the feature well. Typing std::, you'll get a popup list.

Incompatibility with clang_complete

Note that Omnicppcomplete is incompatible with another Vim plugin called clang_complete, due to its "unconditional popup of completion list". To understand this, the following pictures give a good interpretation.

In Omnicppcomplete, after typing std::, the completion list will pop up.
However, in clang_complete, since the information is from the clang compiler, you need to give the syntax in the right position for the compiler so as to let the completion list to show up.

Missing #include<iostream>
Still wrong position of the input
Right position
In Ubuntu, it's easy to install. Note that you need clang-dev as well. For Win*, maybe you need to download the source. (I hope there will be a compiled version of the compiler.)

Reference:
[1]: http://hi.baidu.com/dt_zhangwei/item/277572044909fc103a53eea5