[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#define
And#include
The 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#include
D 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.c
Run the Preprocessing Program and compile it into the target file.foo.o。
-c
The 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。
-o
Indicates 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.out
Name.
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. In
In 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.a
Or. 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-Wall
Option:
% gcc -Wall -c foo.cc
-Wall
Option 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-prototypes
The option is to enable the compiler to warn functions that do not have a correct prototype in the code.-ansi
And-pedantic
Is 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 gcc
At 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 ongcc
Can 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
.