📄 chapter02.html
字号:
been more consistent and less confusing (but would have required more typing,
which probably explains the decision).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here are some more examples of
declarations:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C02:Declare.cpp</font>
<font color=#009900>// Declaration & definition examples</font>
<font color=#0000ff>extern</font> <font color=#0000ff>int</font> i; <font color=#009900>// Declaration without definition</font>
<font color=#0000ff>extern</font> <font color=#0000ff>float</font> f(<font color=#0000ff>float</font>); <font color=#009900>// Function declaration</font>
<font color=#0000ff>float</font> b; <font color=#009900>// Declaration & definition</font>
<font color=#0000ff>float</font> f(<font color=#0000ff>float</font> a) { <font color=#009900>// Definition</font>
<font color=#0000ff>return</font> a + 1.0;
}
<font color=#0000ff>int</font> i; <font color=#009900>// Definition</font>
<font color=#0000ff>int</font> h(<font color=#0000ff>int</font> x) { <font color=#009900>// Declaration & definition</font>
<font color=#0000ff>return</font> x + 1;
}
<font color=#0000ff>int</font> main() {
b = 1.0;
i = 2;
f(b);
h(i);
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In the function declarations, the
argument identifiers are optional. In the definitions, they are required (the
identifiers are required only in C, not C++).</FONT><BR></P></DIV>
<A NAME="Heading77"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Including headers </H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most libraries contain significant
numbers of functions and variables. To save work and ensure consistency when
making the external declarations for these items, C and C++ use a device called
the <I>header file<A NAME="Index312"></A></I>. A header file is a file
containing the external declarations for a library; it conventionally has a file
name extension of ‘h’, such as <B>headerfile.h</B>. (You may also
see some older code using different extensions, such as <B>.hxx</B> or
<B>.hpp</B>, but this is becoming rare.)</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The programmer who creates the library
provides the header file. To declare the functions and external variables in the
library, the user simply includes the header file. To include a header file, use
the <B>#include</B>
<A NAME="Index313"></A><A NAME="Index314"></A><A NAME="Index315"></A>preprocessor<A NAME="Index316"></A>
directive. This tells the preprocessor to open the named header file and insert
its contents where the <B>#include</B> statement appears. A <B>#include</B> may
name a file in two ways: in angle brackets (<B>< ></B>) or in double
quotes. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">File names in angle brackets, such
as:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include <header></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">cause the preprocessor to search for the
file in a way that is particular to your implementation, but typically
there’s some kind of “include search path” that you specify in
your environment or on the compiler command line. The mechanism for setting the
search path varies between machines, operating systems, and C++ implementations,
and may require some investigation on your part.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">File names in double quotes, such
as:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include <font color=#004488>"local.h"</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">tell the preprocessor to search for the
file in (according to the specification) an “implementation-defined
way.” What this typically means is to search for the file relative to the
current directory. If the file is not found, then the include directive is
reprocessed as if it had angle brackets instead of quotes. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To include the iostream header file, you
write:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include <iostream></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The preprocessor will find the iostream
header file (often in a subdirectory called “include”) and insert
it. </FONT><A NAME="_Toc312374158"></A><BR></P></DIV>
<A NAME="Heading78"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Standard C++ include
format<BR><A NAME="Index317"></A><A NAME="Index318"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As C++ evolved, different compiler
vendors chose different extensions for file names. In addition, various
operating systems have different restrictions on file names, in particular on
name length. These issues caused source code portability problems. To smooth
over these rough edges, the standard uses a format that allows file names longer
than the notorious eight characters and eliminates the extension. For example,
instead of the old style of including <B>iostream.h</B>, which looks like
this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include <iostream.h></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">you can now write:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include <iostream></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The translator can implement the include
statements in a way that suits the needs of that particular compiler and
operating system, if necessary truncating the name and adding an extension. Of
course, you can also copy the headers given you by your compiler vendor to ones
without extensions if you want to use this style before a vendor has provided
support for it.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The libraries that have been inherited
from C are still available with the traditional ‘<B>.h</B>’
extension. However, you can also use them with the more modern C++ include style
by prepending a “<B>c</B>” before the name. Thus: </FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include <stdio.h>
#include <stdlib.h></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">become:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#include <cstdio>
#include <cstdlib></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">And so on, for all the Standard C
headers. This provides a nice distinction to the reader indicating when
you’re using C versus C++ libraries.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The effect of the new include format is
not identical to the old: using the <B>.h</B> gives you the older, non-template
version, and omitting the <B>.h</B> gives you the new templatized version.
You’ll usually have problems if you try to intermix the two forms in a
single
program.</FONT><A NAME="_Toc462979719"></A><A NAME="_Toc472654723"></A><BR></P></DIV>
<A NAME="Heading79"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Linking<A NAME="Index319"></A> </H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The linker collects object modules (which
often use file name extensions like <B>.o</B> or <B>.obj</B>), generated by the
compiler, into an executable program the operating system can load and run. It
is the last phase of the compilation process.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Linker characteristics vary from system
to system. In general, you just tell the linker the names of the object modules
and libraries you want linked together, and the name of the executable, and it
goes to work. Some systems require you to invoke the linker yourself. With most
C++ packages you invoke the linker through the C++ compiler. In many situations,
the linker is invoked for you invisibly.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Some older linkers
<A NAME="Index320"></A><A NAME="Index321"></A>won’t search object files
and libraries more than once, and they search through the list you give them
from left to right. This means that the order of object files and libraries can
be important. If you have a mysterious problem that doesn’t show up until
link time, one possibility is the order in which the files are given to the
linker.</FONT><A NAME="_Toc462979720"></A><A NAME="_Toc472654724"></A><BR></P></DIV>
<A NAME="Heading80"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Using libraries <A NAME="Index322"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now that you know the basic terminology,
you can understand how to use a library. To use a library:</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Include the
library’s header
file.</FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Use the
functions and variables in the
library.</FONT><LI><FONT FACE="Verdana"> </FONT><FONT FACE="Georgia">Link the
library into the executable
program.</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">These steps also
apply when the object modules aren’t combined into a library. Including a
header file and linking the object modules are the basic steps for separate
compilation in both C and C++.</FONT><BR></P></DIV>
<A NAME="Heading81"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
How the linker searches a library </H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you make an external reference to a
function or variable in C or C++, the linker, upon encountering this reference,
can do one of two things. If it has not already encountered the definition for
the function or variable, it adds the identifier to its list of
“unresolved
references<A NAME="Index323"></A><A NAME="Index324"></A>.” If the linker
has already encountered the definition, the reference is
resolved.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If the linker cannot find the definition
in the list of object modules, it searches the libraries<A NAME="Index325"></A>.
Libraries have some sort of indexing so the linker doesn’t need to look
through all the object modules in the library – it just looks in the
index. When the linker finds a definition in a library, the entire object
module, not just the function definition, is linked into the executable program.
Note that the whole library isn’t linked, just the object module in the
library that contains the definition you want (otherwise programs would be
unnecessarily large). If you want to minimize executable program size, you might
consider putting a single function in each source code file when you build your
own libraries. This requires more
editing</FONT><A NAME="fnB27" HREF="#fn27">[27]</A><A NAME="Index326"></A><A NAME="Index327"></A><FONT FACE="Georgia">,
but it can be helpful to the user.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Because the linker searches files in the
order you give them, you can pre-empt the use of a library function
<A NAME="Index328"></A>by inserting a file with your own function, using the
same function name, into the list before the library name appears. Since the
linker will resolve any references to this function by using your function
before it searches the library, your function is used instead of the library
function. Note that this can also be a bug, and the kind of thing C++ namespaces
prevent.</FONT><BR></P></DIV>
<A NAME="Heading82"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Secret additions</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When a C or C++ executable program is
created, certain items are secretly linked in. One of these is the startup
module<A NAME="Index329"></A>, which contains initialization routines that must
be run any time a C or C++ program begins to execute. These routines set up the
stack and initialize certain variables in the program.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The linker always searches the standard
library <A NAME="Index330"></A>for the compiled versions of any
“standard” functions called in the program. Because the standard
library is always searched, you can use anything in that library by simply
including the appropriate header file in your program; you don’t have to
tell it to search the standard library. The iostream functions, for example, are
in the Standard C++ library. To use them, you just include the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -