[Translation] C track: compiling C programs., trackcompiling

Source: Internet
Author: User

[Translation] C track: compiling C programs., trackcompiling

Original article: C track: compiling C programs.

C track: compiling C programs.

Although some computer languages (such as Schema or Basic) usually use an interactive interpreter (which can be executed immediately after you enter a command), the C language is not. C's source file always needs to be compiled into binary code through a program called the compiler (compiler) and then run. This is the detailed steps.

Several different types of files

You need four types of files to compile C Programs:

There are other types of files, Especially static library files (".a"Files or". lib "on Windows) and shared library files (".so"Files or". dll "on Windows ). But generally, you do not need to deal with them directly.

Preprocessing

Before the compiler starts to compile the source file, the source file is pre-processor (Preprocessor). A pre-processor is a real separate program (usually called"cpp", For" C preprocessor "), which is automatically called by the compiler before compilation. The job of the pre-processor is to convert the source file into another source file (you can also think of it as a modification or extension of the source file ). The modified file may exist in the file system as a real file, or it may only be reserved in memory before being sent to the compiler. In addition, you do not need to pay special attention to preprocessing, but you need to know what preprocessing is.

Pre-processing commands with symbols ("#"). There are two most important pre-processing commands:

There are other pre-processing commands that we will handle as needed.

Generate the target file: Compiler

The C Preprocessor contains all header files and expands all#defineAnd#includeThe compiler can compile the program after the statement (or some other preprocessing commands appear in the source file. The compiler compiles the C source file into the target file (Object code ),Files that contain the source code of the binary version and end with ". o. However, the target file cannot be run directly. To generate executable files, you also need to add#includeD contains the Library Function Code (this is different from the # include function declaration ). This is the work of the linker in the next section.

Generally, compilation is called in the following ways:

    % gcc -c foo.c

Symbol%Is a unix prompt. It tells the compilerfoo.cRun the Preprocessing Program and compile it into the target file.foo.o。 -cThe option means that the compiler will compile the source file into the target file without calling the linker. If your entire program has a source file, you can do the same:

    % gcc foo.c -o foo

It tells the CompilerFoo. c runs the pre-processor, compiles and links to generate an executable file foo。-oIndicates that the binary executable file (Program) uses its subsequent words as the file name. If you do not specify-O option, or just input Gcc foo. c. For some historical reason, the executable file a.outName.

Note the name of the compiler. We usegcc,Represents "gnu c compiler" or "GNU compiler collection ". There are also other compilers; most of them useCc ("C compiler") Name. InIn LinuxCc is The alias of gcc.

Knead into a group: Linker

The work of the linker is to link a set of target files (. o files) to a binary executable file. This includes the target files compiled from your source code files and pre-compiled library files (Library files ).These files.aOr. So is the end name. Generally, you don't need to know them, because most of them can be named by the linker (Linker) located and automatically linked as needed.

Like a Preprocessor, the linker is also a program called ld independent. Like a Preprocessor, the linker is automatically called when you use the compiler. The method used by the linker is as follows:

    % gcc foo.o bar.o baz.o -o myprog

This line tells the compiler to combine the three target files (foo.o,bar.o, Andbaz.o)Link toBinary executable file of myprog.

This is what you need to know how to compile your C program. We also recommend-WallOption:

    % gcc -Wall -c foo.cc

-WallOption to enable the compiler to warn about valid but usable code structures and help you easily capture some buckets. If you want more compilation check items:

    % gcc -Wall -Wstrict-prototypes -ansi -pedantic -c foo.cc

 -Wstrict-prototypesThe option is to enable the compiler to warn functions that do not have a correct prototype in the code.-ansiAnd-pedanticIs a structure that makes the compiler unportable to the Code (E.g.Some code structures that are valid in gcc but do not meet the standard C compilers; these structures are usually to be avoided) to warn.

References
  • Kernighan and Ritchie, The C Programming Language, 2nd Ed.

  • The man pagegcc. Type:man gccAt the unix prompt.

  • The GNU Info documentation ongcc.Warning!This is far more information than most people cocould possibly absorb in the average millenium.

    Info documentation ongccCan be accessed through the GNU emacs editor by typing "M-x info" (where "M-x" means to hit the meta-key and "x" simultaneously ), or "C-h I" (where "C-h" means to hit the control key and "I" simultaneously), followed by "mgcc <return> ". type "minfo <return>" instead for a quick tour of how to use info. you can also access the info documentation from the unix command line by typinginfo gcc.

Related Article

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.