C language compilation process and CC usage

Source: Internet
Author: User

To generate an executable file, follow these steps:

  1. Preprocessing your source code, removing comments, and other technical work is like developing a macro in C.

  2. Check the code syntax to see if you have followed the rules of this language. If not, the compiler will give a warning.

  3. Converting source code to assembly language-similar to machine code, but we can still understand it under certain circumstances. [1]

  4. Convert assembly language to machine language-yes, we are talking about bitwise AND byte, that is, 1 and 0.

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

  6. If you compile multiple source code files, you must learn how to combine these files.

  7. Load the generated items into memory and run them with the system running loader.

  8. Finally, write the executable file to the file system.

  CompileThis term usually refers to steps 1 to 4-Other steps are calledConnection. Sometimes Hou's first step is calledPreprocessing. Steps 3 and 4 are calledAssembly.

Fortunately, almost all of these details are hidden, becauseCCOnly a front-end. It calls the program based on the correct parameters to process the code. Just enter

%

CC foobar. c

ThenFoobar. cCompile the SDK using the preceding steps. If you have multiple files to compile, just enter

%

CC Foo. c bar. c

Note: syntax check is purely a check syntax. Without detecting any logical errors you may make. For example, an infinite loop or a bubble sequence is used when you want to sort data by one dollar. [2]

  CCThere are many options that can be found in the help manual. Some of the most important options are listed here, and there are examples.

-o filename

Output file name. If you do not use this option,CCTo generateA. Out. [3]

%CC foobar. cThe executable file isA. Out%CC-O foobar. cThe executable file isFoobar

-c

Only files are compiled and not connected. This option is useful if you only want to check the syntax of the test program you wrote. Or you will useMakefile.

%CC-C foobar. c

This will generateTarget File(Not executable) is calledFoobar. o. This file can be connected with other target files to form an executable file.

-g

Generate an executable file that can be debugged. The compiler inserts some information into the executable file, which can associate the number of rows in the source file with the called function. When you debug the program step by step, the debugger can use this information to display the source code. This is very useful; the disadvantage is that the implanted information makes the program larger. We usually use it when developing a program.-gBut when we compile a "release version" program, if the program works well, we will not use it.-gCompile.

%CC-G foobar. c

This will generate a program that can be debugged. [4]

-O

Generate an executable file of the optimized version. The compiler uses some clever techniques to generate executable files that are generated faster than normal compilation. You can-OAdd numbers to use more advanced optimization. However, this often exposes some errors in the compiler optimizer. For exampleCCIn some cases-O2Then, an error code is generated.

Optimization is usually enabled only when a release version is compiled.

%CC-o foobar. c

This will generate an optimized versionFoobar.

-O and-O1 Specify level 1 Optimization

-O2: Level 2 Optimization

-O3: Level 3 Optimization

-The O0 parameter is not optimized.

$ CC-C O3-O0 hello. c

When there are multiple optimizations, take the last one as the standard !!

-I

You can specify other locations for the include file. For example, if some include files are in special locations, such as/usr/local/include, you can add the following options:

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

In this case, the Directory Search is performed in the given order.

 

-E

This option is relatively standard. It allows you to modify the command line so that the compiler sends the pre-processed C file to the standard output without actually compiling the code. this is useful when viewing C pre-processing pseudo commands and C macros. possible compilation outputs can be redirected to a file again and analyzed using the editing program:

 

$ CC-c-e hello. c> CPP. Out

This command causes the include file and program to be pre-processed and redirected to the file CPP. Out. Later, you can use the Edit Program or paging command to analyze the file and determine what the final C language code looks like.

-D

Allow defining macro characters from the compiled program command line

 

There are two cases: one is using-dmacro, which is equivalent to using # define macro in the program, and the other is using-dmacro = A, which is equivalent

# Define macro A in the program, as shown in the following code:

# Ifdefine debug

Printf ("Debug message \ n ");

# Endif

The-ddebug parameter can be added during compilation, and the execution program prints the compilation information.

The following three parameters will forceCCCheck whether your Code complies with some international standards. It is often called ANSI.Standard, although strictly speaking it is an ISOStandard.

-Wall

Open allCCThe author believes that warning is worth noting. Do not just look at the name of this option, it does not open allCCAll warnings that can be noticed.

-ansi

Close most, but not all,CCNon-ANSIC features. Do not just look at the option name. It does not strictly ensure that your code is compatible with the standard.

-pedantic

Close allCCNon-ANSIC features.

None of these options,CCAllows you to use some non-standard extensions according to the standard. Some extensions are useful but not compatible with other compilers -- in fact, one of the main purposes of this standard is to allow us to write code that can be compiled by any compiler on any system. This is calledPortable code

Generally, you should make your code as portable as possible. Otherwise, you have to completely rewrite your code to run it elsewhere -- and who knows if you will use it a few years later?

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.