c language compilation process and use cc

Generate an executable file usually go through the following steps:

  1. Pretreatment your source code, remove the comments, and other work skills of like expanding macros in C.

  2. Code syntax checking to see if you follow the rules of the language. If not, the compiler will give a warning.

  3. The source code into machine code and assembly language ── very similar, but in certain cases we can still understand. [1]

  4. The assembly language into machine language ── Yes, we are talking about bits and bytes, that is, 1 and 0.

  5. Check that you have correctly used similar functions and global variables thing. For example, if you call a function that does not exist, the compiler will give a warning.

  6. If you are from multiple source code files to compile, you must learn how to combine the files together.

  7. Things with generation system loader runs out into memory and run.

  8. Finally, the executable file is written to the file system.

  Compiling meaning of this word generally refers ── other steps Step 1-4 is called connected . Sometimes the first step is called pre-processing . The third and fourth step is called compilation .

  Fortunately, almost all of these details are hidden because cc is just a front end. It is to deal with the code according to the correct parameters caller. Just enter

%

cc foobar.c

  It will put foobar.c compiled by the steps above. If you have more than one file to compile, just type

%

cc foo.c bar.c

  Note that the syntax checking is ── pure syntax checking. The logic does not detect any mistakes you might make. For example, an infinite loop, or you want to use one yuan was used to sort bubble sort. [2]

  cc have a lot of options, can be found in the help manual. Here are some of the most important options, and there are examples.

-o filename

The output file name. If you do not use this option, CC to produce a called a.out executable file. [3]

% cc foobar.c The executable file is a.out % cc -o foobar foobar.c The executable file is foobar
-c

Just compile the file, it will not be connected. If you just want to check the syntax of your written test procedures, then this option is useful. Or you can use the Makefile .

% cc -c foobar.c

This creates a target file (non-executable) is called foobar.o . This file can be other object files and connected together to form an executable file.

-g

Generate a debug executable file. The compiler will implant some of the information in the executable file, the information can be put in the source file and line number of the called function linked. When you step by step debugger, the debugger can use this information to display the source code. This is very useful; drawback is implanted make the program more information. Under normal circumstances, the development of a program when we often use -g, but we compile a "release version" of procedures, if the program works very satisfactory, and we will not use -gthe compiler.

% cc -g foobar.c

This will produce a debug version of the program. [4]

-O

Produce an optimized version of the executable file. The compiler will use some clever tricks to generate the executable file to perform faster than ordinary file generated by the compiler. It can -Obe used in more advanced optimization digitally. But doing so often exposed optimizer compiler some errors. For example, version 2.1.0 of FreeBSD cc used in some cases -O2, it will generate an error code.

Optimization is usually only was only open in compiling a release version of the time.

% cc -O -o foobar foobar.c

This produces an optimized version of foobar .

-O and -O1 designated Level 1 Optimization

Specify -O2 optimization level 2

Specify -O3 optimization level 3

-O0 not specify optimization

$cc -c O3 -O0 hello.c

When multiple optimization occurs, subject to a final !!

-I

Can be specified include file to find other locations for example, if some include files are located in special places, such as / usr / local / include, you can add this option as follows:

$cc -c -I/usr/local/include -I/opt/include hello.c

At this point directory search will be carried out in the order given.

 

-E

This option is relatively standard, which allows to modify the command line so that the C compiler pre-processed document sent to the standard output, without actually compiled code. In view C C preprocessor directives and macro, which is useful possible compiler output can be redirected to a file, and then edit the program to analyze:

 

$cc -c -E hello.c >cpp.out

This command causes the include files and programs are pre-processed and redirected to the file cpp.out. This file can be analyzed later editing program or page break command, and determines the final look how the C language code.

-D

It allows the compiler command line defines a macro symbol

 

There are two cases: one is -Dmacro, is equivalent to using #define MACRO program, the other is to use -DMACRO = A, corresponding to
#Define MACRO A. The procedure as described for the code following this:

#ifdefine DEBUG

printf("debug message\n");

#endif

Compile dissecting -DDEBUG parameters, then print out the execution of the program compiled information

  The following three parameters will force cc to check your code meets international standards, we are often called the ANSI standard, though strictly speaking it is an ISO standard.

-Wall

Open all cc authors believe is worth noting warnings. Despite the name of this option, it does not open all cc can be noted that all warnings.

-ansi

Turn off most, but not all, CC non provided ANSI C features. Despite the name option, it does not guarantee you a strict code compatibility standards.

-pedantic

Close all cc Africa ANSI C features.

  Without these options, CC allow you to use some non-standard extensions in accordance with the standard. There are some very useful extensions, but not compatible with other compilers ── In fact, one of the main purpose of this standard is to allow us to write code that can be compiled on any system from any compiler. This is called portable code

  In general, you should make your code as much as possible transplant. Otherwise you will have to completely rewrite your code to be able to a few years later and who knows whether you will use it in other places ── run it?

http://bbzhang.blogbus.com/logs/47083175.html

Reproduced in: https: //www.cnblogs.com/kungfupanda/archive/2013/03/04/2942439.html

Guess you like

Origin blog.csdn.net/weixin_34252090/article/details/94493273