Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Linux Getting Started tutorial: GNU C and Vim will fight the C / C ++ IDE semi-automatic     - HomeKit User Interface Guidelines (Linux)

- Build Nginx + uWSGI + Flask operating environment under CentOS 6.4 tutorial (Server)

- Zabbix configuration of SNMP (Server)

- How to modify the SQL Server auto-increment value and the corresponding solution (Database)

- Linux dual physical network card set to a virtual NIC (Linux)

- Ubuntu modify locale problem solving (Linux)

- Use small network command to check whether PC Security (Linux)

- ASM Disk Space Check (Database)

- Oracle: RETURNING clause (Database)

- Use GLOBK batch command network adjustment (Linux)

- SecureCRT remote connection Ubuntu fails to solve the case (Linux)

- Let VMware ESXi virtual switch support VLAN (Linux)

- Making Linux root file system problems on-link library (Programming)

- How to install and configure in Ubuntu 14.10 'Weather Information Indicator' (Linux)

- Oracle table space rename and delete table space (Database)

- Oracle 11g forget approach SYS and SYSTEM password (Database)

- Oracle table space is too large processing time (Database)

- Create Your Own Docker base image in two ways (Linux)

- hexdump Linux command (Linux)

- Linux, modify the fstab file system can not start causing solve one case (Linux)

  Linux Getting Started tutorial: GNU C and Vim will fight the C / C ++ IDE semi-automatic
  Add Date : 2018-11-21      
  The importance of the C language in Linux system is naturally unique, irreplaceable, so I wrote a series of rivers and lakes Linux is impossible not to mention the C language. C language is my first language, thanks to lead me into the C language program in the world. While it is not relied on to eat, but still inevitably often deal with it, especially in the Linux system.

Linux systems are commonly used GNU-C, there is a Gnu-C language manual .pdf

The GNU C Reference Manual homepage here: http: //www.gnu.org/software/gnu-c-manual/. Core C language is extremely compact, the handbook total of only 91, only 70 to remove the directory, after the appendices and index. I usually more than an hour from start to finish can be reviewed again. I've had it translated into Chinese ideas, then gave up. Translation of this carefully worded things or let someone else do it. I only write about my own perception.

Sentiment 1: C language standard dry, but GNU extensions

 Recently in order to study the underlying X Window protocol, I began to try to use XCB programming. When I opened XCB header files, I was shocked a lot of __restrict__ keyword, fortunately there GNU C language manual for my FAQ. __restrict__ it is a GNU extension keyword, later I will explain in detail the use of keywords. In fact, C99 standard C language have been introduced restrict keyword, not underlined before and after, but in a large number of open source code, the most commonly used is the GNU extensions, rather than the standard C language.

And restrict keyword has the same fate as well as inline, _Complex, etc., they are introduced in the C99 standard keywords, but in fact, before the C99 standard out, GNU C has long been __inline __, __ complex__ and other key expansion word. I remember many years ago when learning Linux 0.11 version of the source code and see a lot of __inline__ have endless confusion, do not know why Linus in 91 years will be able to spend so advanced language features, and later learned that this is the GNU extended keywords.

Standard C language of C89 and C99, or even use GCC to display the specified -std = c99 to fully support C99 standard, so in the open source community, we still like the preferred GNU extension keyword. For example __inline __, __ complex__ and __restrict__. All in all, C language standard dry, but GNU extensions.

Let's look at the true meaning of __restrict__. Contained an article "Why Some languages other than fast" "For a long time, the same two runs, which refers to the Fortran and C (or C ++) in, Fortran will be faster, because Fortran Optimization to do better. this is true, even if the C language compiler and Fortran Used the same code generator is the same. this is not because of some different characteristics of Fortran, in fact just the opposite, because Fortran does not have the characteristics. "this is because a pointer to the C language compiler optimization makes it difficult, the article continued:." the problem comes these pointers can replace any memory address more importantly, they can overlap the output array. memory address that can be both the input array. You can even partially overlap, the output array can cover a half of the input array. this compiler optimization is a big problem, because before array-based optimization is no longer applicable. in particular, add order of the elements is also a problem, if the output of overlapping array of calculated results will become uncertain, depending on the output element of the action occurs before or after the element to be covered. "

With __restrict __, the C language will cease to exist. . __restrict__ Modified with a pointer, the pointer 1 that can only be initialized when defined; 2. Not another pointer to a pointer to the memory, so the compiler can optimize the algorithm. The following code:

int * __restrict__ p = (int *) malloc (100 * sizeof (int));
Pointer p have __restrict__ keyword modification, so it can only be initialized when defined, can not be assigned in the future, but not __restrict__ modified pointer can be assigned at any time, as follows:

int arr [100];
int * pArr;
pArr = arr;
PArr pointer is not modified __restrict__ keywords, so the first address of the array can be assigned to it.

For example, we define a function of two data operation, the result into the first three memory, as follows:

void func1 (void * p1, void * p2, void * p3, int size) {
    for (int i = 0; i         p3 [i] = p1 [i] + p2 [i];
Obviously, because the compiler can not determine pointer p1, p2, p3 point memory is overlap, it can not be optimized, plus __restrict__ after keyword, as follows:

void func1 (void * __restrict__ p1, void * __restrict__ p2, void * __restrict__ p3, int size) {
    for (int i = 0; i         p3 [i] = p1 [i] + p2 [i];
This is equivalent to explicitly tell the compiler that a few memory do not overlap, so the compiler can safely optimize the program.

 Another key is _Complex, C99 was introduced, and the need to include the header file. In fact, the GNU C, have long been __complex __, __ real __, __ imag__ keywords and other extensions. The following code:

 1 #include < stdlib.h >
 2 #include < stdio.h >
 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;
You can see that in the C language can also be calculated directly on the complex. Numerical Fortran no longer patent.

Sentiment 2: pointers and arrays really are not the same

Starting to learn C language, the teacher taught us that pointers and arrays are the same, they can be operated in the same way. In fact, pointers and arrays are still differences. Until years later read "C Programming experts", was called up as pointers and arrays is a beautiful mistake, simply because "The C Programming Language" in this book, the "time as a function of parameters, pointers and arrays as" this before and after the sentence separately are printed to two only.

For example, the pointer does not save the data length information, and there is an array, the following code:

 1 #include < stdlib.h >
 2 #include < stdio.h >
 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;
Operating results of this code is:

The size of p: 8
The size of arr: 400
We often use the following code fragment to obtain a number of elements in the array, as follows:

int arr [100];
size_t length = sizeof (arr) / sizeof (int);
However, when using an array as a parameter when the array will degenerate into a pointer. The following code:

 1 #include
 2 #include
 4 void test_array (int arr []) {
 5 printf ( "The of arr size in function:% d \ n", sizeof (arr));
 6 return;
 9 int main () {
10 int arr [100] = {0};
11 printf ( "The of arr size in main:% d \ n", sizeof (arr));
12 test_array (arr);
13 return 0;
Operating results of this code is:

The size of arr in main: 400
The size of arr in function: 8
Sentiment 3: C language incomplete type (Incomplete Types)

 In GNU C you can define the type of incomplete, imperfect, there are two main types, one is empty structures, one array is empty, for example:

struct point;
char name [0];
Empty structure can not define variables, can only use the pointer empty structure. Empty structure may be added later and then it complete, as follows:

struct point {
    int x, y;
Hollow structure defining the list of commonly used when, as follows:

struct linked_list {
    struct linked_list * next;
    int x;
    / * Other elements here perhaps * /
struct linked_list * head;

There is an incomplete type is the last one to define a structure for an empty array, which can be used to represent a variable or array length to demonstrate the technology code is as follows:

 1 #include < stdlib.h >
 2 #include < stdio.h >
 4 typedef struct {
 5 int length;
 6 int arr [0];
 7} incomplete_type;
 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 length; i ++) {
15 p-> arr [i] = hello [i];
17 printf ( "p-> length =% d \ n", p-> length);
18 printf ( "p-> arr =% s \ n", p-> arr);
To build C / C ++ in the IDE

Behind the content shows how Vim playing a semi-automatic C / C ++ IDE. Java read my blog friends should know that, in fact, I prefer to use Eclipse. Only in the need to write a very simple program (such as doing exercises) situation, I will use Vim. There are discussed in my "build their own Vim" in. In this article I showed you how to use Vundle management plug-ins and how how to read the help file, at the same time shows a simple usage of taglist.vim. If you want Vim to write C / C ++ program, you need to do a little expansion.

First, install the following plug-ins, since Vundle management plug-in, so just need to plug-in name written .vimrc configuration file, then run: BundleInstall to

We were introduced about these plug-ins. The-NERD-tree is a browser plug-in directory and files, you can use: help NERD_tree.txt see its help documentation. taglist.vim browsing symbols and symbols to jump between the plug-in, use: help taglist.txt see its help documentation. a.vim is between the source and header files Jump plug, you do not need help document that commandment is: A. c.vim main function is to provide IDE plug-ins, the functionality it provides automatic annotation, anti Notes automatically insert blocks of code and run automatically, if installed splint, the code can also be static checking, use: help csupport.txt View its documentation. OmniCppComplete is a provider of auto-completion of the plug-in, use: help omnicppcomplete.txt see its documentation.

These plug-ins, and support taglist.vim OmniCppComplete need ctags software, so you need to install exuberant-ctags packages in Fedora 20, you only need to use yum install ctags automatically installed.

Second, generate tags database, and added to Vim.

We write C program, use the file to mainly exists in two places, one is the current directory of our work, the other is / usr / include. So go to the next / usr / include directory using the ctags command to generate tags database file. To make the tags database contains as much information (structures, enumerations, classes, functions, macros, and so on), you need to specify the parameters of ctags

Then the path tags file is added to the .vimrc configuration file, and set a keyboard map, so press Ctrl + F12, the calling ctags command in the working directory. The last two lines of the configuration file as follows

Then, use Vim to write C program, if you enter -> such elements, its members will be auto-complete. If the input is a string (such as function names), you can press Ctrl-X Ctrl-O automatic call completion

Not only candidate window will pop up, and the full signature function at the top of the window displays, and the file is located. This we often not remember the name of the function, the function signature remember people who already is a great gospel.

taglist.vim and OmniCppComplete plug-ins provide functionality only need to use up a command and the command c.vim provided relatively more. And all functions commands c.vim help document and is not listed, there is a way to learn these commands, it is open GVim, by GVim menu C / C ++ to learn c.vim menu provides functions and commands .

Compared to other online Vim will fight IDE article, my configuration is relatively simple, basically just installed a few plugins, and did not do too many settings. When I need a function, I'll use the command explicitly call it, so called semi-automatic IDE it.
- MySQL 5.6 master-slave replication configuration (Database)
- PostgreSQL Select source code analysis (Database)
- How to set IonCube Loaders in Ubuntu (Linux)
- Cobbler batch install Ubuntu / CentOS system (Linux)
- JBoss7 configuration - Supports IPv4 and IPv6 dual-stack environment (Server)
- Node.js installed on Ubuntu Upstream version (Linux)
- MySQL Study of --Percona Server 5.5 Upgrade 5.6 (Database)
- To configure linux transparent firewall (Linux)
- MySQL completely uninstall and install Configuring Character Sets under Linux (Database)
- and localhost difference (Server)
- How Glances monitoring system on Ubuntu (Linux)
- Oracle to create an external table (Database)
- SQLite (Database)
- GNU Linux use diff to generate a patch with the patch (Linux)
- Linux kernel source code analysis (Linux)
- Oracle online redefinition (Database)
- Linux SU command security Suggestions (Linux)
- Installation and operation GAMIT software under Linux operating system (Linux)
- Java object serialization (Programming)
- Supervisor Installation and Configuration (Server)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.