编译C++ 程序的过程
2014-08-06 20:48
267 查看
编译C++ program的时候, 需要work with 4 kinds of file.
(1) regular source code files: 含有function definitions等, 后缀是.cpp
(2)header files: 含有function declarations(又称function prototypes), 以及许多不同的preprocessor statements. 这些预编译器指导语句 used to allow source code files to access externally-defined functions. Header files end in "
(一般而言是这样的, 不过不同系统可能后缀名不同)by convention.
(3)object files: 目标文件是Compiler 输出的。 consist of function definitions in binary form, but they are not executable by themselves。 Object files end in "
by convention(一般而言是这样的, 不过不同系统可能后缀名不同)by convention.)
(4) binary executables:这是我们的program linker 输出的文件。 The linker links together a number of object files to produce a binary file which can be directly executed. Binary executables have no special suffix on Unix
operating systems, although they generally end in "
it is invoked automatically by the compiler before compilation proper begins. What the preprocessor does is convert the source code file you write into another source code file (you can think of it as a "modified" or "expanded" source code file). That modified
file may exist as a real file in the file system, or it may only be stored in memory for a short time before being sent to the compiler. Either way, you don't have to worry about it, but you do have to know what the preprocessor commands do.
Preprocessor commands start with the pound sign ("
specifies that wherever the character string
becomes
to change it once, in the
causes the preprocessor to paste the contents of
files which mainly contain function declarations and
file
There are a number of other preprocessor commands as well, but we will deal with them as we need them.
file), the compiler can compile the program. It does this by turning the C source code into an object code file, which is a file ending in "
executable, though. In order to make an executable, you also have to add code for all of the library functions that were
is the job of the linker (see the next section).
In general, the compiler is invoked as follows:
where
means to compile the source code file into an object file but not to invoke the linker. If your entire program is in one source code file, you can instead do this:
This tells the compiler to run the preprocessor on
is the name of the binary executable file (program). If you don't specify the
Note also that the name of the compiler we are using is
the name
as object files that have been pre-compiled for you and collected into library files. These files have names which end in
them are located and will link them in automatically as needed.
Like the preprocessor, the linker is a separate program called
is as follows:
This line tells the compiler to link together three object files (
you can run and which will hopefully do something cool and/or useful.
This is all you need to know to begin compiling your own C programs. Generally, we also recommend that you use the
The
The
the compiler to warn about any non-portable construct (e.g. constructs that may be legal in
(1) regular source code files: 含有function definitions等, 后缀是.cpp
(2)header files: 含有function declarations(又称function prototypes), 以及许多不同的preprocessor statements. 这些预编译器指导语句 used to allow source code files to access externally-defined functions. Header files end in "
.h"
(一般而言是这样的, 不过不同系统可能后缀名不同)by convention.
(3)object files: 目标文件是Compiler 输出的。 consist of function definitions in binary form, but they are not executable by themselves。 Object files end in "
.o"
by convention(一般而言是这样的, 不过不同系统可能后缀名不同)by convention.)
(4) binary executables:这是我们的program linker 输出的文件。 The linker links together a number of object files to produce a binary file which can be directly executed. Binary executables have no special suffix on Unix
operating systems, although they generally end in "
.exe" on Windows.
The preprocessor
Before the C compiler starts compiling a source code file, the file is processed by a preprocessor. This is in reality a separate program (normally called "cpp", for "C preprocessor"), but
it is invoked automatically by the compiler before compilation proper begins. What the preprocessor does is convert the source code file you write into another source code file (you can think of it as a "modified" or "expanded" source code file). That modified
file may exist as a real file in the file system, or it may only be stored in memory for a short time before being sent to the compiler. Either way, you don't have to worry about it, but you do have to know what the preprocessor commands do.
Preprocessor commands start with the pound sign ("
#"). There are several preprocessor commands; two of the most important are:
#define. This is mainly used to define constants. For instance,
#define BIGNUM 1000000
specifies that wherever the character string
BIGNUMis found in the rest of the program,
1000000should be substituted for it. For instance, the statement:
int a = BIGNUM;
becomes
int a = 1000000;
#defineis used in this way so as to avoid having to explicitly write out some constant value in many different places in a source code file. This is important in case you need to change the constant value later on; it's much less bug-prone
to change it once, in the
#define, than to have to change it in multiple places scattered all over the code.
#include. This is used to access function definitions defined outside of a source code file. For instance:
#include <stdio.h>
causes the preprocessor to paste the contents of
<stdio.h>into the source code file at the location of the
#includestatement before it gets compiled.
#includeis almost always used to include header files, which are
files which mainly contain function declarations and
#definestatements. In this case, we use
#includein order to be able to use functions such as
printfand
scanf, whose declarations are located in the
file
stdio.h. C compilers do not allow you to use a function unless it has previously been declared or defined in that file;
#includestatements are thus the way to re-use previously-written code in your C programs.
There are a number of other preprocessor commands as well, but we will deal with them as we need them.
Making the object file: the compiler
After the C preprocessor has included all the header files and expanded out all the#defineand
#includestatements (as well as any other preprocessor commands that may be in the original
file), the compiler can compile the program. It does this by turning the C source code into an object code file, which is a file ending in "
.o" which contains the binary version of the source code. Object code is not directly
executable, though. In order to make an executable, you also have to add code for all of the library functions that were
#included into the file (this is not the same as including the declarations, which is what
#includedoes). This
is the job of the linker (see the next section).
In general, the compiler is invoked as follows:
% gcc -c foo.c
where
%is the unix prompt. This tells the compiler to run the preprocessor on the file
foo.cand then compile it into the object code file
foo.o. The
-coption
means to compile the source code file into an object file but not to invoke the linker. If your entire program is in one source code file, you can instead do this:
% gcc foo.c -o foo
This tells the compiler to run the preprocessor on
foo.c, compile it and then link it to create an executable called
foo. The
-ooption states that the next word on the line
is the name of the binary executable file (program). If you don't specify the
-o, i.e. if you just type
gcc foo.c, the executable will be named
a.outfor silly historical reasons.
Note also that the name of the compiler we are using is
gcc, which stands for "GNU C compiler" or "GNU compiler collection" depending on who you listen to. Other C compilers exist; many of them have
the name
cc, for "C compiler". On Linux systems
ccis an alias for
gcc.
Putting it all together: the linker
The job of the linker is to link together a bunch of object files (.ofiles) into a binary executable. This includes both the object files that the compiler created from your source code files as well
as object files that have been pre-compiled for you and collected into library files. These files have names which end in
.aor
.so, and you normally don't need to know about them, as the linker knows where most of
them are located and will link them in automatically as needed.
Like the preprocessor, the linker is a separate program called
ld. Also like the preprocessor, the linker is invoked automatically for you when you use the compiler. The normal way of using the linker
is as follows:
% gcc foo.o bar.o baz.o -o myprog
This line tells the compiler to link together three object files (
foo.o,
bar.o, and
baz.o) into a binary executable file named
myprog. Now you have a file called
myprogthat
you can run and which will hopefully do something cool and/or useful.
This is all you need to know to begin compiling your own C programs. Generally, we also recommend that you use the
-Wallcommand-line option:
% gcc -Wall -c foo.cc
The
-Walloption causes the compiler to warn you about legal but dubious code constructs, and will help you catch a lot of bugs very early. If you want to be even more anal (and who doesn't?), do this:
% gcc -Wall -Wstrict-prototypes -ansi -pedantic -c foo.cc
The
-Wstrict-prototypesoption means that the compiler will warn you if you haven't written correct prototypes for all your functions. The
-ansiand
-pedanticoptions cause
the compiler to warn about any non-portable construct (e.g. constructs that may be legal in
gccbut not in all standard C compilers; such features should usually be avoided).
相关文章推荐
- C/C++程序编译的过程
- C++程序从编译到链接然后再到调用的整个过程
- C++程序编译过程
- 【C/C++】程序如何来,编译全过程
- C++编译过程中"没有找到MFC80UD.DLL,因此这个程序未能启动.重新安装应用程序可能会修复此问题"? 的彻底解决
- c++程序编译过程
- 【C/C++】程序如何来,编译全过程
- C/C++程序编译运行生成过程分析
- Windows系统下用命令行编译C/C++程序过程总结
- 在VS2008中编译纯c/c++程序并由c#调用过程
- C/C++程序的编译过程
- C++程序编译过程
- C/C++程序从编译到最终生成可执行文件的过程分析
- 深度剖析一个典型的C/C++程序的编译、链接以及执行的过程
- C++编译过程中"没有找到MFC80UD.DLL,因此这个程序未能启动.重新安装应用程序可能会修复此问题"? 的彻底解决
- C++编译过程中"没有找到MFC80UD.DLL,因此这个程序未能启动.重新安装应用程序可能会修复此问题"? 的彻底解决
- c++程序编译过程总结(个人理解)
- C/C++程序从编译到最终生成可执行文件的过程分析
- C++程序编译过程简介
- 在VS2008中编译纯c/c++程序并由c#调用过程