Linux getting started Tutorial: gnu c and semi-automated IDE that makes Vim C/C ++
The importance of C language in Linux is naturally unparalleled and irreplaceable. Therefore, it is impossible for me to write the Linux series without mentioning the C language. C language is my first language. Thanks to C language for leading me into the program world. Although I don't rely on it for dinner now, I still have to deal with it frequently, especially in Linux.
In the linuxlinuxsystem, gnu-cis commonly used. There is a gnu-clanguage manual, which is ,:.
------------------------------------------ Split line ------------------------------------------
Free in http://linux.bkjia.com/
The username and password are both www.bkjia.com
The specific download directory is available in/July 6,/July 2,/getting started with Linux. Tutorial: gnu c and semi-automated IDE that makes Vim C & C ++/
For the download method, see
------------------------------------------ Split line ------------------------------------------
The gnu c Reference Manual homepage is here: http://www.gnu.org/software/gnu-c-manual /. The C language kernel is extremely compact, and there are only 91 pages in this manual. After removing directories, appendices, and indexes, there are only 70 pages. Generally, I can review it from the beginning to the end in over an hour. I had the idea of translating it into Chinese, and I gave up later. Let others do the translation. I only write my own feelings.
Perception 1: C language standards do not support GNU Expansion
Recently, in order to study the underlying protocol of X Window, we started to try XCB programming. When I opened the XCB header file, I was stunned by a large number of _ restrict _ keywords. Fortunately, the GNU C language manual answered questions for me. _ Restrict _ is a keyword of GNU extension. I will explain in detail the usage of this keyword later. In fact, the C99 standard of C language has introduced the restrict keyword, without the underline before and after, but in a large number of open source code, the most common is the GNU extension, rather than the C language standard.
Similar to restrict keywords, there are also inline, _ Complex, and so on. They are all keywords introduced in the C99 standard, but before the C99 standard came out, extended keywords such as _ inline _ and _ complex _ are already available in gnu c. I still remember that when I learned the source code of Linux 0.11 many years ago, I was confused when I saw a lot of _ inline, I don't know why Linus was able to use such advanced language functions in. Later I learned that this is the GNU extension keyword.
C language standards include C89 and C99. When using GCC, you even need to display the specified-std = c99 to fully support the C99 standard. Therefore, in the Open Source world, you still prefer the GNU extension keyword. For example, _ inline _, _ complex _, and _ restrict __. All in all, C language standards do not support GNU extensions.
Let's take a look at the true meaning of _ restrict. I wrote an article titled why some languages are faster than others, which mentions that "for a long time, two identical programs run in Fortran and C (or C ++, fortran is faster, because the optimization of Fortran is better. This is true, even if the C language and Fortran compiler use the same code generator. This difference is not because of some characteristics of Fortran. In fact, it is the opposite because it is not a feature of Fortran ." This is because pointers in C Language bring difficulties to compiler optimization. The article continues: "The problem is coming. These pointers can replace any memory address. More importantly, they can overlap. The memory address of the output array can also be an input array. It can even partially overlap. The output array can overwrite half of an input array. This is a big problem for Compiler Optimization Because array-based optimization is no longer applicable. In particular, the order of adding elements is also a problem. If an overlapping array is output, the calculation result will become uncertain, depending on whether the action of the output element occurs before or after the element is overwritten ."
With _ restrict __, the problem of C language will no longer exist. After a pointer is modified with _ restrict _, ① the pointer can only be initialized during definition; ② No other Pointer Points to the memory pointed to by the pointer, therefore, the compiler can optimize the algorithm. The following code:
int * __restrict__ p = (int*)malloc(100*sizeof(int));
The pointer p has the _ restrict _ keyword modifier. Therefore, it can only be initialized during definition and cannot be assigned values in the future without the _ restrict _ modifier pointer. You can assign values at any time, as follows:
int arr[100];int* pArr;pArr = arr;
The pointer pArr is not modified by the _ restrict _ keyword. Therefore, you can assign the first address of the array to it.
For example, if we define a function to operate on two pieces of data, the result is put into 3rd pieces of memory, as shown below:
void func1(void* p1, void* p2, void* p3, int size){ for(int i=0; i<size; i++){ p3[i] = p1[i] + p2[i]; }}
Obviously, the compiler cannot determine whether the memory pointed to by pointers p1, p2, and p3 overlap, So optimization cannot be performed. After the _ restrict _ keyword is added, the following is returned:
void func1(void* __restrict__ p1, void* __restrict__ p2, void* __restrict__ p3, int size){ for(int i=0; i<size; i++){ p3[i] = p1[i] + p2[i]; }}
It is equivalent to clearly telling the compiler that the memory does not overlap, so the compiler can be confident in program optimization.
The other keyword is _ Complex, which is introduced by C99 and must contain the header file <complex. h>. In gnu c, extended keywords such as _ complex _, _ real _, and _ imag _ already exist. The following code:
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 int main(){ 5 __complex__ a = 3 + 4i; 6 __complex__ b = 5 + 6i; 7 __complex__ c = a + b; 8 __complex__ d = a * b; 9 __complex__ e = a / b;10 printf("a + b = %f + %fi\n", __real__ c, __imag__ c);11 printf("a * b = %f + %fi\n", __real__ d, __imag__ d);12 printf("a / b = %f + %fi\n", __real__ e, __imag__ e);13 return 0;14 }
As you can see, the C language can also directly calculate the plural. Numerical Computation is no longer a Fortran patent.
Perception 2: pointers and arrays are really different.
Since learning C language, the teacher taught us that pointers and arrays are the same and they can be operated in the same way. In fact, there is a difference between pointers and arrays. It wasn't until many years later that I read "C expert Programming" until The so-called pointer and array were a beautiful mistake, just because in The book The C Programming Language, we printed a sentence like "pointer and array as function parameters" on two separate pages.
For example, the pointer does not save the Data Length information, but the array has the following code:
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 int main(){ 5 int* p = (int*)malloc(100*sizeof(int)); 6 int arr[100] = {0}; 7 printf("The size of p: %d\n", sizeof(p)); 8 printf("The size of arr: %d\n", sizeof(arr)); 9 return 0;10 }
The running result of this Code is:
The size of p: 8The size of arr: 400
We can often use the following code snippet to obtain the number of elements in an array, as shown below:
int arr[100];size_t length = sizeof(arr)/sizeof(int);
However, when an array is used as a function parameter, the array degrades to a pointer. The following code:
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 void test_array(int arr[]){ 5 printf("The size of arr in function: %d\n", sizeof(arr)); 6 return; 7 } 8 9 int main(){10 int arr[100] = {0};11 printf("The size of arr in main: %d\n", sizeof(arr));12 test_array(arr);13 return 0;14 }
The running result of this Code is:
The size of arr in main: 400The size of arr in function: 8
Sentiment 3: Incomplete Types in C Language (Incomplete Types)
In gnu c, incomplete types can be defined. There are two types: NULL structure and empty array. For example:
struct point;char name[0];
A null structure cannot define variables. Only null structure pointers can be used. You can complete the empty structure later, as shown below:
struct point{ int x,y;};
Empty structures are often used when defining linked lists, as follows:
struct linked_list{ struct linked_list* next; int x; /*other elements here perhaps */}struct linked_list* head;
Another incomplete type is to define the last item of a structure as an empty array, which can be used to represent a variable-length structure or array. The code to demonstrate this technology is as follows:
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 typedef struct { 5 int length; 6 int arr[0]; 7 } incomplete_type; 8 9 int main(){10 char hello[] = "Hello, world!";11 int length = sizeof(hello) / sizeof(char);12 incomplete_type* p = (incomplete_type*)malloc(sizeof(int) + length*sizeof(char));13 p->length = length;14 for(int i=0; i<p->length; i++){15 p->arr[i] = hello[i];16 }17 printf("p->length=%d\n", p->length);18 printf("p->arr=%s\n", p->arr);19 }
Build C/C ++ IDE
The following content demonstrates how to build Vim into a semi-automatic C/C ++ IDE. Friends who have read my Java blog should know that I prefer Eclipse. Vim is used only when you need to write very simple programs (such as exercises. This is discussed in my "create your own Vim. In this article, I demonstrate how to use the Vundle management plug-in and how to read the help documentation, and the simple usage of taglist. vim. If you want to use Vim to write C/C ++ programs, you also need to make a few extensions.
First, install the following plug-ins. Because the Vundle management plug-in is used, you only need to write the plug-in name to the. vimrc configuration file, and then run: BundleInstall, for example:
These plug-ins are described separately. The-NERD-tree is a plug-in for viewing directories and files. You can use: help NERD_tree.txt to view its help documentation. Taglist. vim is a plug-in for browsing symbols and redirecting between symbols. You can use: help taglist.txt to view its help documentation. A. vim is A plug-in that redirects between the source code file and the header file. It does not need help documentation. Its command is. C. vim is the main plug-in that provides IDE functions, including automatic comments, reverse comments, automatic insertion of code blocks, and automatic running. If splint is installed, you can also perform static checks on the code, use: help csupp.txt .txt to view its documentation. OmniCppComplete is a plug-in that provides automatic complementing functions. You can use: help omnicppcomplete.txt to view its documentation.
In these plug-ins, taglist. vim and OmniCppComplete require the support of the ctags software. Therefore, you need to install the exuberant-ctags package. In Fedora 20, you only need to use yum install ctags to automatically install it.
Second, generate the tags database and add it to Vim.
When we write a C program, the files used mainly exist in two places, one is the current directory where we work, and the other is/usr/include. Therefore, use the ctags command in the/usr/include directory to generate the tags database file. To make the tags database contain as much information as possible (structure, enumeration, class, function, macro definition, etc.), you need to specify the ctags parameter as follows:
Add the path of the tags file to the. vimrc configuration file, and set a keyboard ing so that the ctags command is called in the working directory when Ctrl + F12 is pressed. The last two lines of the configuration file are as follows:
Then, when using Vim to write a C program, if an element such as. And-> is input, its members are automatically completed. If the input is a string (such as a function name), you can press Ctrl-X Ctrl-O to call auto-completion, for example:
In addition, the candidate window is displayed, and the complete signature of the function and its file are displayed in the top window. This is a great boon to those who often do not remember the function name or function signature.
The functions provided by taglist. vim and OmniCppComplete plug-ins only require one command, while c. vim provides more commands. And in c. the vim Help document does not list all functional commands. One way to learn these commands is to open GVim and use the C/C ++ menu in the GVim menu to learn c. functions and commands provided by vim.
Compared to other articles on the Internet that make Vim an IDE, my configuration is relatively simple, basically only a few plug-ins are installed, without too many settings. When I need a function, I will use the command to explicitly call it. So, call it semi-automated IDE.
-------------------------------------- Split line --------------------------------------
Build VIM into a simple and practical IDE
Vim Learning Guide
Quick learn Vi Editor
Powerful Vim Editor
Build a Vim Development Environment on CentOS 6.2
Install the highlighted Vim editing tool in CentOS 5.4
Vim tips: C language settings
Set the Vim row number in Ubuntu
Vim editor basic tutorial
-------------------------------------- Split line --------------------------------------
This article permanently updates the link address: