Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Vim into a semi-automatic IDE of CumberCraft +

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to make Vim a semi-automated IDE for C/C++". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and study and learn "how to make Vim a semi-automated IDE for C/C++" together.

Understanding 1: C language standard can not do GNU extension

Recently, in order to study the underlying protocol of X Window, I began to try to use XCB programming. When I opened the header file of XCB, I was stunned by the large number of__restrict__keywords. Fortunately, the GNU C language manual answered my questions.__ Restrict__is another GNU extension keyword that I'll explain in detail later. In fact, the C language C99 standard has introduced the restrict keyword, there is no underscore before and after, but in a large number of open source code, the most common use of GNU extensions, rather than the C language standard.

The same fate as the restrict keyword is inline,_Complex, etc. They are all keywords introduced in the C99 standard, but in fact, before the C99 standard came out, GNU C had already had the extension keywords__inline__,__complex__, etc. I remember years ago when I was studying Linux 0.11 source code, I saw a lot of__inline__and wondered why Linus could use such advanced language features in 1991. Later, I learned that this is an extension keyword of GNU.

C language standards have C89 and C99, when using GCC even to specify-std=c99 to fully support the C99 standard, so in the open source community, we still prefer GNU extension keywords. Such as__inline__,__complex__and__restrict__. In short, the C standard can't beat GNU extensions.

Let's see what__restrict_really means. Remember the article on CSDN,"Why some languages are faster than others," which said,"For a long time, the same two programs ran in Fortran and C (or C++), Fortran was faster because Fortran was optimized better." This is true even if the C and Fortran compilers use the same code generator. This difference is not due to some feature of Fortran, but rather to features Fortran does not have. "This is because pointers in C make it difficult for compilers to optimize," the article continues."Here's the problem. These pointers can replace any memory address. More importantly, they can overlap. The memory address of the output array can also be the memory address of the input array. It can even overlap partially, with the output array covering half of an input array. This is a big problem for compiler optimizations because previous array-based optimizations no longer apply. In particular, the order in which elements are added is problematic; if overlapping arrays are output, the result of the calculation becomes uncertain, depending on whether the action of outputting elements occurs before or after the elements are overwritten. "

With__restrict__, this problem in C will no longer exist. After modifying a pointer with__restrict__,(1) the pointer can only be initialized when defined;(2) no other pointer points to the memory pointed to by the pointer, so the compiler can optimize the algorithm. The code is as follows:

The code is as follows:

int * __restrict__ p = (int*)malloc(100*sizeof(int));

Pointer p has the__restrict__keyword modifier, so it can only be initialized when defined, and cannot be assigned later, while pointers without the__restrict__modifier can be assigned at any time, as follows:

The code is as follows:

int arr[100];

int* pArr;

pArr = arr;

Pointer pArr is not decorated with the__restrict__keyword, so it can be assigned the first address of an array.

For example, we define a function to operate on two pieces of data, and the result is placed in the third piece of memory, as follows:

The code is as follows:

void func1(void* p1, void* p2, void* p3, int size){

for(int i=0; iarr[i] = hello[i];

}

printf("p->length=%d\n", p->length);

printf("p->arr=%s\n", p->arr);

}

Creating C/C++ IDE

The following shows how to make Vim a semi-automatic C/C++ IDE. Those of you who have read my Java blog should know that I prefer Eclipse. I use Vim only when I need to write very simple programs, such as doing exercises. This is discussed in my book Creating Your Own Vim. In this article I show how to use the Vundle admin plug-in and how to read the help documentation, as well as simple usage of taglist.vim. If you want to write C/C++ programs in Vim, you need to do a little extension.

First, install the following plug-ins. Since you use Vundle to manage plug-ins, you only need to write the plug-in name to the.vimrc configuration file, and then run:BundleInstall, as shown below:

Let's talk about these plugins separately. The-NERD-tree is a directory and file browsing plugin whose help documentation can be viewed using: helpNERD_tree.txt. taglist.vim is a plug-in for browsing symbols and jumping between symbols. Use: helptaglist.txt to view its help documentation. a.vim is a plug-in that jumps between source code files and header files without help documentation. Its command is:A. c.vim is the main plug-in that provides IDE functionality, including autocomment, anticomment, autoblock insertion, autorun, and static code inspection if splint is installed. Use: helpcsupport.txt to view its documentation. OmniCppComplete is a plug-in that provides autocomplete functionality. Use: helpomnicppcomplete.txt to view its documentation.

Among these plugins, taglist.vim and OmniCppComplete require ctags software support, so you need to install exuberant-ctags software package. In Fedora 20, you only need to use yum install ctags to install them automatically.

Second, generate the tags database and add it to Vim.

When we write C programs, the files we use exist primarily in two places: the current directory where we work and/usr/include. So go to/usr/include directory and use ctags command to generate tags database file. In order for the tags database to contain as much information as possible (structures, enumerations, classes, functions, macro definitions, etc.), you need to specify the parameters of ctags, as follows:

Then add the path to the tags file to the.vimrc configuration file and set a keyboard mapping so that when Ctrl+F12 is pressed, the ctags command is invoked in the working directory. The last two lines of the following configuration file:

Then, when writing C programs using Vim, if you enter., -> Such elements are automatically completed by their members. If you enter a string (such as a function name), you can press Ctrl-X Ctrl-O to call autocompletion, as shown below:

Not only will candidate windows pop up, but the full signature of the function and its file will be displayed in the topmost window. This is a great boon for those of us who often cannot remember the full name of a function or the signature of a function.

The taglist.vim and OmniCppComplete plug-ins provide functionality that requires only one command to use, whereas c.vim provides more commands. There is also no list of commands for all functions in the c.vim help documentation. One way to learn these commands is to open GVim and learn the functions and commands provided by c.vim through the C/C++ menu in the GVim menu.

Compared to other articles on the Internet that make Vim an IDE, my configuration is relatively simple, basically only installing a few plug-ins, and not doing too many settings. When I need a feature, I explicitly invoke it with a command, so call it a semi-automated IDE.

Thank you for reading, the above is "how to make Vim into C/C++ semi-automated IDE" content, after the study of this article, I believe that everyone on how to make Vim into C/C++ semi-automated IDE this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report