⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch52_08.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 4 页
字号:
% <CODECLASS="userinput"><B>mv pcal.man /usr/local/man/man1/pcal.1</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Note, however, that some programs may have extra steps in installingthe executable.If all this worked as advertised, you can bail out now.Otherwise,you might need to know more about what goes on behind the scenesbefore you can figure out what went wrong.</P></DIV><DIVCLASS="sect3"><H4CLASS="sect3"><ACLASS="title"NAME="UPT-ART-3840-SECT-1.2.7">52.8.2.7 Functions, Libraries, and Header Files </A></H4><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63747"></A>To understand the compilation process, it helps to understand a little about libraries and header files.  </P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63750"></A>C programs are written almost entirely using <EMCLASS="emphasis">functions</EM>.Article<ACLASS="xref"HREF="ch15_03.htm"TITLE="Adding { } Operators to Korn (and Bourne) Shells ">15.3</A>shows an example of a function defined in the Bourne shell programming language.C language functions are basically the same idea:group together a series of commands, give them a name, and then you can execute those commands using that name whenever you want andas many times as you want.Functions are also sometimes referred toas <EMCLASS="emphasis">subroutines</EM>, <EMCLASS="emphasis">library functions</EM>, or just <EMCLASS="emphasis">routines</EM>. </P><PCLASS="para">Now, you can define C functions in the same source file.But the operating system also provides a vast collection of function definitions&nbsp;- which is very nice, because otherwiseyou'd be building every program from scratch.The functiondefinitions are kept in <EMCLASS="emphasis">libraries</EM>, which are generally installed on your system in <EMCLASS="emphasis">/usr/lib/</EM> with a <EMCLASS="emphasis">lib</EM> prefixand a <EMCLASS="emphasis">.a</EM> suffix (for example, <EMCLASS="emphasis">/usr/lib/libc.a</EM>).</P><PCLASS="para">Functions also have to be <EMCLASS="emphasis">declared</EM> in theprogram.Function declarations are kept in <EMCLASS="emphasis">header</EM> or<EMCLASS="emphasis">include</EM> files, which are generally installed on your system in<EMCLASS="emphasis">/usr/include/</EM> with <EMCLASS="emphasis">.h</EM> suffixes (for example,<EMCLASS="emphasis">/usr/include/stdio.h</EM>).</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63772"></A>If you use functions that are defined in libraries (and you mostdefinitely will), you need to make sure that when the program iscompiled, it is <EMCLASS="emphasis">linked</EM> to the libraries it needs.You also haveto make sure that the proper header files are read by your program,since the program won't compile unless all functions have beendeclared.  </P><PCLASS="para">For example, if you need to take the square root of a number in yourprogram, you need to use the <EMCLASS="emphasis">sqrt()</EM> function.This functionresides in the Math library.This means thatyou need to link the program with <EMCLASS="emphasis">libm.a</EM> and you need to read inthe <EMCLASS="emphasis">math.h</EM> header file (which declares <EMCLASS="emphasis">sqrt()</EM>).Soin the program, you need to have the following line near the top of thesource file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">#include &lt;math.h&gt;</PRE></BLOCKQUOTE></P><PCLASS="para">and when you compile the program, you need to use the <EMCLASS="emphasis">-l</EM>(lowercase L) command-line option to link with <EMCLASS="emphasis">libm</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cc -o </B></CODE><CODECLASS="replaceable"><I>file file</I></CODE><CODECLASS="userinput"><B>.c -lm</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">&#13;Note the following facts:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63795"></A>Unless you name the executable file with <CODECLASS="literal">-o&nbsp;</CODE><CODECLASS="replaceable"><I>file</I></CODE>,<EMCLASS="emphasis">cc</EM> will name it <EMCLASS="emphasis">a.out</EM>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63803"></A>The source filename must end with a <EMCLASS="emphasis">.c</EM> suffix.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63808"></A><ACLASS="indexterm"NAME="AUTOID-63811"></A>Since <EMCLASS="emphasis">math.h</EM> lives in <EMCLASS="emphasis">/usr/include</EM>, you don't need to giveits absolute pathname on the <CODECLASS="literal">#include</CODE> line, just put the nameof the header file between angle brackets as shown.<SPANCLASS="link">Relative pathnames (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>starting at <EMCLASS="emphasis">/usr/include</EM> can be used in angle brackets.For instance, <CODECLASS="literal">&lt;sys/foo.h&gt;</CODE> means <EMCLASS="emphasis">/usr/include/sys/foo.h</EM>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63822"></A><ACLASS="indexterm"NAME="AUTOID-63824"></A>By default,<EMCLASS="emphasis">cc</EM> looks for header files in <EMCLASS="emphasis">/usr/include</EM>, and you canhave it look automatically in other directories by specifyingthem with the <EMCLASS="emphasis">-I</EM> command-line option.If you want to use a header file in directory that isn'tsearched by default, supply itsabsolute or relative pathname in double quotes instead.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63832"></A>When linking with a library on the command line, you should putthe <EMCLASS="emphasis">-l</EM> options at the end. If you use more than one library, you'll need more than one<EMCLASS="emphasis">-l</EM> option.The order of the <EMCLASS="emphasis">-l</EM> options is important; check thedocumentation or look for a comment in the source code.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63840"></A>The compiler found <EMCLASS="emphasis">libm.a</EM> because it was in <EMCLASS="emphasis">/usr/lib</EM>,which it searches by default.If you want it to use a libraryin another directory, you may need to supply the directory using the <EMCLASS="emphasis">-L</EM>command-line option.</P></LI></UL><PCLASS="para">As you can imagine, there's much more to know.But that's thegeneral idea of compiling C programs on UNIX systems, and it'sabout as much as we can tell you without starting to teach you C.</P></DIV><DIVCLASS="sect3"><H4CLASS="sect3"><ACLASS="title"NAME="UPT-ART-3840-SECT-1.2.8">52.8.2.8 The make Program </A></H4><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63850"></A>When you're writing a simple C program, you can simply compile theprogram using <EMCLASS="emphasis">cc</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cc test.c</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">But more complicated programs (like many of the programs on the CD-ROM) require a bit more work.More complicatedprograms are easier to handle if you write them in <EMCLASS="emphasis">modules</EM>.So, for example, the <EMCLASS="emphasis">pcal</EM> source tree on the CD-ROMcontains several <EMCLASS="emphasis">.c</EM> files: <EMCLASS="emphasis">exprpars.c</EM>, <EMCLASS="emphasis">moonphas.c</EM>, <EMCLASS="emphasis">pcalinit.c</EM>, <EMCLASS="emphasis">pcalutil.c</EM>, <EMCLASS="emphasis">readfile.c</EM>,<EMCLASS="emphasis">writefil.c</EM>, and, of course, <EMCLASS="emphasis">pcal.c</EM>.Each of thesesource files needs to be compiled separately into <EMCLASS="emphasis">object files</EM>(with <EMCLASS="emphasis">.o</EM> suffixes).If you give the <EMCLASS="emphasis">-c</EM> option, <EMCLASS="emphasis">cc</EM> will compile&quot;<EMCLASS="emphasis">.c</EM> files&quot; into &quot;<EMCLASS="emphasis">.o</EM> files&quot; and stop without making thefinished executable.When you run <EMCLASS="emphasis">cc</EM> again&nbsp;- but give it the <EMCLASS="emphasis">.o</EM> filenames(<EMCLASS="emphasis">exprpars.o</EM>, <EMCLASS="emphasis">moonphas.o</EM>, and so on)it will link all those object files with the libraries and make theexecutable file.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63879"></A>This makes compilation a bit harder to keep track of.There are a lotmore steps.Furthermore, it means that whenever a file is changed, you have to remember not only to recompile it but also to relinkthe entire program.  </P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63882"></A><ACLASS="indexterm"NAME="AUTOID-63884"></A>This is a job for the <EMCLASS="emphasis">make</EM> program.We showed uses for <EMCLASS="emphasis">make</EM>in articles <ACLASS="xref"HREF="ch21_09.htm"TITLE="Keep File Printouts Up-to-Date Automatically with make">21.9</A>and <ACLASS="xref"HREF="ch28_13.htm"TITLE="make Isn't Just for Programmers! ">28.13</A>,but this is what it was really meant for.The <EMCLASS="emphasis">pcal</EM> source tree comes with a file called <EMCLASS="emphasis">Makefile</EM>.(Actually, it comes with several different <EMCLASS="emphasis">Makefile</EM>s fordifferent platforms, but that's another issue.)The <EMCLASS="emphasis">Makefile</EM>keeps track of each of the programs and each of their dependencies.It also keeps track of any command-line options you might want passedto <EMCLASS="emphasis">cc</EM>, including libraries to link to.The result is that when you want to make the <EMCLASS="emphasis">pcal</EM> program,all you need to do is type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>make pcal</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Or, even better, just:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>make</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This is a lot easier than trying to keep track of all the modules andcommand-line options yourself.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63906"></A>So if you can't compile a program because the header file it needs isinstalled in a non-standard place, you'd specify that in the<EMCLASS="emphasis">Makefile</EM>.You could add the appropriate <EMCLASS="emphasis">-I</EM>option to the <CODECLASS="literal">COPTS</CODE> declaration line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">COPTS    = -I/usr/include/sys</PRE></BLOCKQUOTE></P><PCLASS="para">Or if you want to use a different compiler than <EMCLASS="emphasis">cc</EM>, you couldredefine that variable:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">CC       = /usr/local/bin/gcc</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-63919"></A><ACLASS="indexterm"NAME="AUTOID-63921"></A><ACLASS="indexterm"NAME="AUTOID-63922"></A>Again, this is only the tip of the iceberg.But a basic understandingof libraries, header files, and<EMCLASS="emphasis">make</EM> has helped me build many programs that wouldn't compilethe first time.For help with <EMCLASS="emphasis">make</EM>, see O'Reilly &amp; Associates' <EMCLASS="emphasis">Managing Projectswith make</EM> by Andrew Oram and Steve Talbott.</P></DIV></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">LM</SPAN>, <SPANCLASS="authorinitials">EP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch52_07.htm"TITLE="52.7 Other Ways to Get the Software "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 52.7 Other Ways to Get the Software "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch52_09.htm"TITLE="52.9 Software Support from RTR "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 52.9 Software Support from RTR "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">52.7 Other Ways to Get the Software </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">52.9 Software Support from RTR </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -