|
Lines 1-7
Link Here
|
| 1 |
<!-- |
1 |
<!-- |
| 2 |
The FreeBSD Documentation Project |
2 |
The FreeBSD Documentation Project |
| 3 |
|
3 |
|
| 4 |
$FreeBSD: doc/zh_TW.Big5/books/developers-handbook/tools/chapter.sgml,v 1.1 2006/06/17 10:22:44 vanilla Exp $ |
4 |
$FreeBSD$ |
|
|
5 |
Original revision: 1.46 |
| 5 |
--> |
6 |
--> |
| 6 |
|
7 |
|
| 7 |
<chapter id="tools"> |
8 |
<chapter id="tools"> |
|
Lines 19-145
Link Here
|
| 19 |
</authorgroup> |
20 |
</authorgroup> |
| 20 |
</chapterinfo> |
21 |
</chapterinfo> |
| 21 |
|
22 |
|
| 22 |
<title>Programming Tools</title> |
23 |
<title>µ{¦¡¶}µo¤u¨ã</title> |
| 23 |
<sect1 id="tools-synopsis"><title>Synopsis</title> |
24 |
<sect1 id="tools-synopsis"><title>·§±Ô</title> |
| 24 |
|
25 |
|
| 25 |
<para>This chapter is an introduction to using some of the |
26 |
<para> |
| 26 |
programming tools supplied with FreeBSD, although much of it |
27 |
¥»³¹±N¤¶²Ð¦p¦ó¨Ï¥Î¤@¨Ç FreeBSD ©Ò´£¨Ñªºµ{¦¡¶}µo¤u¨ã(programing tools)¡A |
| 27 |
will be applicable to many other versions of &unix;. It does |
28 |
¥»³¹©Ò¤¶²Ðªº¤u¨ãµ{¦¡¦b¨ä¥Lª©¥»ªº &unix; ¤W¤]¥i¨Ï¥Î¡A |
| 28 |
<emphasis>not</emphasis> attempt to describe coding in any |
29 |
¦b¦¹ <emphasis>¨Ã¤£·|</emphasis> ¹Á¸Õ´yz¼gµ{¦¡®Éªº¨CÓ²Ó¸`¡A |
| 29 |
detail. Most of the chapter assumes little or no previous |
30 |
¥»³¹¤j³¡¤À½g´T³£¬O°²³]§A¥H«e¨S¦³©Î¥u¦³¤Ö¼Æªº¼gµ{¦¡¸gÅç¡A |
| 30 |
programming knowledge, although it is hoped that most |
31 |
¤£¹L¡AÁÙ¬O§Æ±æ¤j¦h¼Æªºµ{¦¡¶}µo¤Hû³£¯à±q¤¤«·s±o¨ì¤@¨Ç±Òµo¡C |
| 31 |
programmers will find something of value in it.</para> |
32 |
</para> |
| 32 |
|
33 |
|
| 33 |
</sect1> |
34 |
</sect1> |
| 34 |
|
35 |
|
| 35 |
<sect1 id="tools-intro"><title>Introduction</title> |
36 |
<sect1 id="tools-intro"><title>²¤¶</title> |
| 36 |
|
37 |
|
| 37 |
<para>FreeBSD offers an excellent development environment. |
38 |
<para> |
| 38 |
Compilers for C, C++, and Fortran and an assembler come with the |
39 |
FreeBSD ´£¨Ñ¤@Ó«D±`´Îªº¶}µoÀô¹Ò¡A |
| 39 |
basic system, not to mention a Perl interpreter and classic &unix; |
40 |
¤ñ¦p»¡¹³¬O C¡BC++¡BFortran ©M assembler(²Õ¦X»y¨¥)ªº½s;¹(compiler), |
| 40 |
tools such as <command>sed</command> and <command>awk</command>. |
41 |
¦b FreeBSD ¤¤³£¤w¸g¥]§t¦b°ò¥»ªº¨t²Î¤¤¤F |
| 41 |
If that is not enough, there are many more compilers and |
42 |
§ó§O´£ Perl ©M¨ä¥L¼Ð·Ç &unix; ¤u¨ã¡A¹³¬O<command>sed</command> ¥H¤Î <command>awk</command>¡A |
| 42 |
interpreters in the Ports collection. FreeBSD is very |
43 |
¦pªG§AÁÙ¬Oı±o¤£°÷¡AFreeBSD¦b Ports collection ¤¤ÁÙ´£¨Ñ¨ä¥Lªº½s;¹©Mª½Ä¶¾¹(interpreter), |
| 43 |
compatible with standards such as <acronym>&posix;</acronym> and |
44 |
FreeBSD ¬Û®e³\¦h¼Ð·Ç¡A¹³¬O <acronym>&posix;</acronym> ©M <acronym>ANSI</acronym> C¡A |
| 44 |
<acronym>ANSI</acronym> C, as well with its own BSD heritage, so |
45 |
·íµMÁÙ¦³¥¦©ÒÄ~©Óªº BSD ¶Ç²Î¡C |
| 45 |
it is possible to write applications that will compile and run |
46 |
©Ò¥H¦b FreeBSD ¤W¼gªºµ{¦¡¤£»Ý×§ï©Î³»¦hµy·L×§ï¡A´N¥i¥H¦b³\¦h¥¥x¤W½sĶ¡B°õ¦æ¡C |
| 46 |
with little or no modification on a wide range of |
47 |
</para> |
| 47 |
platforms.</para> |
48 |
|
| 48 |
|
49 |
<para> |
| 49 |
<para>However, all this power can be rather overwhelming at first |
50 |
µL½×¦p¦ó¡A´Nºâ§A±q¨Ó¨S¦b &unix; ¥¥x¤W¼g¹Lµ{¦¡¡A¤]¥i¥H¹ý©³·P¨ü¨ìFreeBSD ¥O¤HµLªk§Ü©Úªº°g¤HÅ]¤O¡C |
| 50 |
if you have never written programs on a &unix; platform before. |
51 |
¥»³¹ªº¥Ø¼Ð´N¬O¨ó§U§A§Ö³t¤W¤â¡A¦Ó¼È®É¤£»Ý²`¤J¤Ó¦h¶i¶¥¥DÃD¡A |
| 51 |
This document aims to help you get up and running, without |
52 |
¨Ã¥BÁ¿¸Ñ¤@¨Ç°ò¦·§©À¡A¥HÅý§A¥i¥HÁA¸Ñ§Ú̦bÁ¿¨Ç¤°»ò¡C |
| 52 |
getting too deeply into more advanced topics. The intention is |
53 |
</para> |
| 53 |
that this document should give you enough of the basics to be |
54 |
|
| 54 |
able to make some sense of the documentation.</para> |
55 |
<para> |
| 55 |
|
56 |
¥»³¹¤º®e¨Ã¤£n¨D§A±o¦³µ{¦¡¶}µo¸gÅç¡A©ÎªÌ§A¥u¦³¤@ÂIÂIªº¸gÅç¦Ó¤w¡C |
| 56 |
<para>Most of the document requires little or no knowledge of |
57 |
¤£¹L¡A§Ṵ́²³]§A¤w¸g·| &unix; ¨t²Îªº°ò¥»¾Þ§@¡A |
| 57 |
programming, although it does assume a basic competence with |
58 |
¦Ó¥B§ó«nªº¬O¡A½Ð«O«ù¼Ö©ó¾Ç²ßªº¤ßºA¡I |
| 58 |
using &unix; and a willingness to learn!</para> |
59 |
</para> |
| 59 |
|
60 |
|
| 60 |
</sect1> |
61 |
</sect1> |
| 61 |
|
62 |
|
| 62 |
<sect1 id="tools-programming"> |
63 |
<sect1 id="tools-programming"> |
| 63 |
<title>Introduction to Programming</title> |
64 |
<title>Programming ·§©À</title> |
| 64 |
|
65 |
|
| 65 |
<para>A program is a set of instructions that tell the computer to |
66 |
<para> |
| 66 |
do various things; sometimes the instruction it has to perform |
67 |
²³æªº»¡¡Aµ{¦¡¥u¬O¤@°ï«ü¥Oªº¶°¦XÅé¡F¦Ó³o¨Ç«ü¥O¬O¥Î¨Ó§i¶D¹q¸£À³¸Ón§@¨º¨Ç¨Æ±¡¡C |
| 67 |
depends on what happened when it performed a previous |
68 |
¦³®ÉÔ¡A«ü¥Oªº°õ¦æ¨ú¨M©ó«e¤@Ó«ü¥Oªºµ²ªG¦Ó©w¡C |
| 68 |
instruction. This section gives an overview of the two main |
69 |
¥»³¹±N·|§i¶D§A¦³ 2 Ó¥Dnªº¤èªk¡AÅý§A¥i¥H¹ï¹q¸£¤U¹F³o¨Ç«ü¥Ü(instruction) ©Î <quote>©R¥O(commands)</quote>¡C |
| 69 |
ways in which you can give these instructions, or |
70 |
²Ä¤@Ó¤èªk´N¬O <firstterm>ª½Ä¶¾¹(interpreter)</firstterm>¡A |
| 70 |
<quote>commands</quote> as they are usually called. One way |
71 |
¦Ó²Ä¤GÓ¤èªk¬O <firstterm>½s;¹(compiler)</firstterm>¡C |
| 71 |
uses an <firstterm>interpreter</firstterm>, the other a |
72 |
¥Ñ©ó¹ï©ó¹q¸£¦Ó¨¥¡A¤HÃþ»y¨¥ªº»y·N¹L©ó¼Ò½k¦Ó¤ÓÃø²z¸Ñ¡A |
| 72 |
<firstterm>compiler</firstterm>. As human languages are too |
73 |
¦]¦¹©R¥O(commands)´N±`·|¥H¤@ºØ(©Î¦hºØ)µ{¦¡»y¨¥¼g¦¨¡A¥Î¨Ó«ü¥Ü¹q¸£©Òn°õ¦æªº¯S©w°Ê§@¬°¦ó¡C |
| 73 |
difficult for a computer to understand in an unambiguous way, |
74 |
</para> |
| 74 |
commands are usually written in one or other languages specially |
|
|
| 75 |
designed for the purpose.</para> |
| 76 |
|
75 |
|
| 77 |
<sect2> |
76 |
<sect2> |
| 78 |
<title>Interpreters</title> |
77 |
<title>ª½Ä¶¾¹</title> |
| 79 |
|
78 |
|
| 80 |
<para>With an interpreter, the language comes as an environment, |
79 |
<para> |
| 81 |
where you type in commands at a prompt and the environment |
80 |
¨Ï¥Îª½Ä¶¾¹®É¡A©Ò¨Ï¥Îªºµ{¦¡»y¨¥´N¹³Åܦ¨¤@Ó·|©M§A¤¬°ÊªºÀô¹Ò¡C |
| 82 |
executes them for you. For more complicated programs, you can |
81 |
·í¦b©R¥O´£¥Ü¦C¤W¥´¤W©R¥O®É¡Aª½Ä¶¾¹·|§Y®É°õ¦æ¸Ó©R¥O¡C |
| 83 |
type the commands into a file and get the interpreter to load |
82 |
¦b¤ñ¸û½ÆÂøªºµ{¦¡¤¤¡A¥i¥H§â©Ò¦³·Q¤U¹Fªº©R¥O²Î²Î¿é¤J¨ì¬YÀɮ׸̱¥h¡A |
| 84 |
the file and execute the commands in it. If anything goes |
83 |
µM«á©I¥sª½Ä¶¾¹¥hŪ¨ú¸ÓÀɮסA¨Ã¥B°õ¦æ§A¼g¦b³oÓÀɮפ¤ªº«ü¥O¡C |
| 85 |
wrong, many interpreters will drop you into a debugger to help |
84 |
¦pªG©Ò¤Uªº«ü¥O¦³¿ù»~²£¥Í¡A¤j¦h¼Æªºª½Ä¶¾¹·|¶i¤J°»¿ù¼Ò¦¡(debugger)¡A |
| 86 |
you track down the problem.</para> |
85 |
¨Ã¥BÅã¥Ü¬ÛÃö¿ù»~°T®§¡A¥H«K¹ïµ{¦¡°£¿ù¡C |
| 87 |
|
86 |
</para> |
| 88 |
<para>The advantage of this is that you can see the results of |
87 |
|
| 89 |
your commands immediately, and mistakes can be corrected |
88 |
<para> |
| 90 |
readily. The biggest disadvantage comes when you want to |
89 |
³oºØ¤è¦¡¦n³B¦b©ó¡G¥i¥H¥ß¨è¬Ý¨ì«ü¥Oªº°õ¦æµ²ªG¡A¥H¤Î¿ù»~¤]¥i¨³³t×¥¿¡C |
| 91 |
share your programs with someone. They must have the same |
90 |
¬Û¹ïªº¡A³Ì¤jªºÃa³B«K¬O·í§A·Q§â§A¼gªºµ{¦¡¤À¨Éµ¹¨ä¥L¤H®É¡A³o¨Ç¤H¥²¶·n¦³¸ò§A¤@¼Ëªºª½Ä¶¾¹¡C |
| 92 |
interpreter, or you must have some way of giving it to them, |
91 |
¦Ó¥B§O§Ñ¤F¡A¥L̤]n·|¨Ï¥Îª½Ä¶¾¹ª½Ä¶µ{¦¡¤~¦æ¡C |
| 93 |
and they need to understand how to use it. Also users may not |
92 |
·íµM¨Ï¥ÎªÌ¤]¤£§Æ±æ¤£¤p¤ß«ö¿ùÁä¡A´N¶i¤J°»¿ù¼Ò¦¡¦Ó¤£ª¾©Ò±¹¡C |
| 94 |
appreciate being thrown into a debugger if they press the |
93 |
´N°õ¦æ®Ä²v¦Ó¨¥¡Aª½Ä¶¾¹·|¨Ï¥Î¨ì«Ü¦hªº°O¾ÐÅé¡A |
| 95 |
wrong key! From a performance point of view, interpreters can |
94 |
¦Ó¥B³oÃþª½Ä¶¦¡µ{¦¡¡A³q±`¨Ã¤£·|¤ñ½s;¹©Ò½sĶªºµ{¦¡ªº§ó¦³®Ä²v¡C |
| 96 |
use up a lot of memory, and generally do not generate code as |
95 |
</para> |
| 97 |
efficiently as compilers.</para> |
96 |
|
| 98 |
|
97 |
<para> |
| 99 |
<para>In my opinion, interpreted languages are the best way to |
98 |
µ§ªÌÓ¤H»{¬°¡A¦pªG§A¤§«e¨S¦³¾Ç¹L¥ô¦óµ{¦¡»y¨¥¡A³Ì¦n¥ý¾Ç¾Ç²ßª½Ä¶¦¡»y¨¥(interpreted languages)¡A |
| 100 |
start if you have not done any programming before. This kind |
99 |
¹³¬O Lisp¡ASmalltalk¡APerl ©M Basic ³£¬O¡A&unix; ªº shell ¹³¬O <command>sh</command> ©M <command>csh</command> |
| 101 |
of environment is typically found with languages like Lisp, |
100 |
¥¦Ì¥»¨´N¬Oª½Ä¶¾¹¡A¨Æ¹ê¤W¡A«Ü¦h¤H³£¦b¥¦Ì¦Û¤v¾÷¾¹¤W¼¶¼g¦U¦¡ªº shell <quote>script</quote>¡A |
| 102 |
Smalltalk, Perl and Basic. It could also be argued that the |
101 |
¨Ó¶¶§Q§¹¦¨¦U¶µ <quote>housekeeping(ºûÅ@)</quote> ¥ô°È¡C |
| 103 |
&unix; shell (<command>sh</command>, <command>csh</command>) is itself an |
102 |
&unix; ¨Ï¥Îõ¾Ç¤§¤@´N¬O´£¨Ñ¤j¶qªº¤p¤u¨ã¡A |
| 104 |
interpreter, and many people do in fact write shell |
103 |
¨Ã¨Ï¥Î shell script ¨Ó²Õ¦X¹B¥Î³o¨Ç¤p¤u¨ã¡A¥H«K¤u§@§ó¦³®Ä²v¡C |
| 105 |
<quote>scripts</quote> to help with various |
|
|
| 106 |
<quote>housekeeping</quote> tasks on their machine. Indeed, part |
| 107 |
of the original &unix; philosophy was to provide lots of small |
| 108 |
utility programs that could be linked together in shell |
| 109 |
scripts to perform useful tasks.</para> |
| 110 |
</sect2> |
104 |
</sect2> |
| 111 |
|
105 |
|
| 112 |
<sect2> |
106 |
<sect2> |
| 113 |
<title>Interpreters available with FreeBSD</title> |
107 |
<title>FreeBSD ´£¨Ñªºª½Ä¶¾¹</title> |
| 114 |
|
108 |
|
| 115 |
<para>Here is a list of interpreters that are available from |
109 |
<para> |
| 116 |
the &os; Ports Collection, with a brief discussion of |
110 |
¤U±³oÃ䦳¥÷ &os; Ports Collection ©Ò´£¨Ñªºª½Ä¶¾¹²M³æ¡AÁÙ¦³°Q½×¤@¨Ç¤ñ¸û¨üÅwªïªºª½Ä¶¦¡»y¨¥</para> |
| 117 |
some of the more popular interpreted languages.</para> |
|
|
| 118 |
|
| 119 |
<para>Instructions on how to get and install applications |
| 120 |
from the Ports Collection can be found in the |
| 121 |
<ulink url="&url.books.handbook;/ports-using.html"> |
| 122 |
Ports section</ulink> of the handbook. |
| 123 |
|
111 |
|
|
|
112 |
<para> |
| 113 |
¦Ü©ó¦p¦ó¨Ï¥Î Ports Collection ¦w¸Ëªº»¡©ú¡A¥i°Ñ¾\ FreeBSD Handbook ¤¤ªº |
| 114 |
<ulink url="&url.books.handbook;/ports-using.html">Ports³¹¸`</ulink>¡C |
| 124 |
<variablelist> |
115 |
<variablelist> |
| 125 |
<varlistentry> |
116 |
<varlistentry> |
| 126 |
<term><acronym>BASIC</acronym></term> |
117 |
<term><acronym>BASIC</acronym></term> |
| 127 |
|
118 |
|
| 128 |
<listitem> |
119 |
<listitem> |
| 129 |
<para>Short for Beginner's All-purpose Symbolic |
120 |
<para>BASIC ¬O Beginner's ALL-purpose Symbolic Instruction Code ªºÁY¼g¡C |
| 130 |
Instruction Code. Developed in the 1950s for teaching |
121 |
BASIC ©ó 1950 ¦~¥N¶}©lµo®i¡A³Ìªì¶}µo³o®M»y¨¥ªº¥Øªº¬O¬°¤F±Ð¾É·í®Éªº¤j¾Ç¾Ç¥Í¦p¦ó¼gµ{¦¡¡C |
| 131 |
University students to program and provided with every |
122 |
¨ì¤F 1980¡A<acronym>BASIC</acronym>¤w¸g¬O«Ü¦h programmer ²Ä¤@ӾDzߪºµ{¦¡»y¨¥¤F¡C |
| 132 |
self-respecting personal computer in the 1980s, |
123 |
¦¹¥~¡ABASIC ¤]¬O Visual Basic ªº°ò¦¡C</para> |
| 133 |
<acronym>BASIC</acronym> has been the first programming |
124 |
|
| 134 |
language for many programmers. It is also the foundation |
125 |
<para>FreeBSD Ports Collection ¤]¦³¦¬¿ý¬ÛÃöªº BASIC ª½Ä¶¾¹¡C |
| 135 |
for Visual Basic.</para> |
126 |
Bywater Basic ª½Ä¶¾¹©ñ¦b <filename role="package">lang/bwbasic</filename>¡C |
| 136 |
|
127 |
¦Ó Phil Cockroft's Basic ª½Ä¶¾¹(¦´Á¤]¥s Rabbit Basic)©ñ¦b <filename role="package">lang/pbasic</filename>¡C |
| 137 |
<para>The Bywater Basic Interpreter can be found in the |
|
|
| 138 |
Ports Collection as |
| 139 |
<filename role="package">lang/bwbasic</filename> |
| 140 |
and the Phil Cockroft's Basic Interpreter |
| 141 |
(formerly Rabbit Basic) is available as |
| 142 |
<filename role="package">lang/pbasic</filename>.</para> |
| 143 |
</listitem> |
128 |
</listitem> |
| 144 |
</varlistentry> |
129 |
</varlistentry> |
| 145 |
|
130 |
|
|
Lines 147-175
Link Here
|
| 147 |
<term>Lisp</term> |
132 |
<term>Lisp</term> |
| 148 |
|
133 |
|
| 149 |
<listitem> |
134 |
<listitem> |
| 150 |
<para>A language that was developed in the late 1950s as |
135 |
<para>LISP ¬O¦b 1950 ¦~¥N¶}©lµo®iªº¤@Óª½Ä¶¦¡»y¨¥¡A¦Ó¥B LISP ´N¬O¤@ºØ |
| 151 |
an alternative to the <quote>number-crunching</quote> |
136 |
<quote>number-crunching</quote> languages(¨³³t¶i¦æ¤j¶q¹Bºâªºµ{¦¡»y¨¥)¡A¦b·í®Éºâ¬O¤@Ó´¶¹Mªºµ{¦¡»y¨¥¡C |
| 152 |
languages that were popular at the time. Instead of |
137 |
LISP ªºªí¹F¤£¬O°ò©ó¼Æ¦r(numbers)¡A¦Ó¬O°ò©óªí(lists)¡C |
| 153 |
being based on numbers, Lisp is based on lists; in fact |
138 |
¦Ó³Ì¯àªí¥Ü¥X LISP ¯S¦âªº¦a¤è´N¦b©ó¡G LISP ¬O <quote>List Processing</quote> ªºÁY¼g¡C |
| 154 |
the name is short for <quote>List Processing</quote>. |
139 |
¦b<acronym>¤H¤u´¼¼z(Artificial Intelligence, AI)</acronym>»â°ì¤W LISP ªº¦U¦¡À³¥Î«D±`´¶¹M¡C</para> |
| 155 |
Very popular in <acronym>AI</acronym> (Artificial Intelligence) |
140 |
|
| 156 |
circles.</para> |
141 |
<para>LISP ¬O«D±`±j®«¥B½ÆÂøªºµ{¦¡»y¨¥¡A¦ý¬O¯ÊÂI¬Oµ{¦¡½X·|«D±`¤j¦Ó¥BÃø¥H¾Þ§@¡C</para> |
| 157 |
|
142 |
|
| 158 |
<para>Lisp is an extremely powerful and sophisticated |
143 |
<para>µ´¤j³¡¤Àªº LISP ª½Ä¶¾¹³£¥i¥H¦b &unix; ¨t²Î¤W¹B§@¡A·íµM &os; ªº Ports Collection ¤]¦³¦¬¿ý¡C |
| 159 |
language, but can be rather large and unwieldy.</para> |
144 |
GNU Common Lisp ¦¬¿ý¦b <filename role="package">lang/gcl</filename>¡A |
| 160 |
|
145 |
Bruno Haible ©M Michael Stoll ªº CLISP ¦¬¿ý¦b <filename role="package">lang/clisp</filename> |
| 161 |
<para>Various implementations of Lisp that can run on &unix; |
146 |
¡A¦¹¥~ CMUCL(¥]§t¤@Ó¤w¸g³Ì¨Î¤Æªº½s;¹)¡A |
| 162 |
systems are available in the Ports Collection for &os;. |
147 |
¥H¤Î¨ä¥L²¤Æª©ªº LISP ª½Ä¶¾¹(¤ñ¦p¥H C »y¨¥¼gªº SLisp¡A¥u¥Î´X¦Ê¦æµ{¦¡½X´N¹ê§@¤j¦h¼Æ Common Lisp ªº¥\¯à) |
| 163 |
GNU Common Lisp can be found as |
148 |
«h¬O¤À§O¦¬¿ý¦b <filename role="package">lang/cmucl</filename> ¥H¤Î |
| 164 |
<filename role="package">lang/gcl</filename>. CLISP |
149 |
<filename role="package">lang/slisp</filename>¡C |
| 165 |
by Bruno Haible and Michael Stoll is available as |
150 |
</para> |
| 166 |
<filename role="package">lang/clisp</filename>. |
|
|
| 167 |
For CMUCL, which includes a highly-optimizing compiler too, or |
| 168 |
simpler Lisp implementations like SLisp, which implements most |
| 169 |
of the Common Lisp constructs in a few hundred lines of C code, |
| 170 |
<filename role="package">lang/cmucl</filename> and |
| 171 |
<filename role="package">lang/slisp</filename> are available |
| 172 |
respectively.</para> |
| 173 |
</listitem> |
151 |
</listitem> |
| 174 |
</varlistentry> |
152 |
</varlistentry> |
| 175 |
|
153 |
|
|
Lines 177-190
Link Here
|
| 177 |
<term>Perl</term> |
155 |
<term>Perl</term> |
| 178 |
|
156 |
|
| 179 |
<listitem> |
157 |
<listitem> |
| 180 |
<para>Very popular with system administrators for writing |
158 |
<para>¹ï¨t²ÎºÞ²zªÌ¦Ó¨¥¡A³Ì·R¥Î perl ¨Ó¼¶¼g scripts ¥HºÞ²z¥D¾÷¡A |
| 181 |
scripts; also often used on World Wide Web servers for |
159 |
¦P®É¤]¸g±`¥Î¨Ó¼g WWW ¥D¾÷¤Wªº <acronym>CGI</acronym> Script µ{¦¡¡C</para> |
| 182 |
writing <acronym>CGI</acronym> scripts.</para> |
160 |
|
| 183 |
|
161 |
<para>Perl ¦b Ports Collection ¤ºªº <filename role="package">lang/perl5</filename>¡C |
| 184 |
<para>Perl is available in the Ports Collection as |
162 |
¦Ó &os; 4.X «h¬O§â Perl ¸Ë¦b <command>/usr/bin/perl</command>¡C</para> |
| 185 |
<filename role="package">lang/perl5</filename> for all |
|
|
| 186 |
&os; releases, and is installed as <command>/usr/bin/perl</command> |
| 187 |
in the base system 4.X releases.</para> |
| 188 |
</listitem> |
163 |
</listitem> |
| 189 |
</varlistentry> |
164 |
</varlistentry> |
| 190 |
|
165 |
|
|
Lines 192-210
Link Here
|
| 192 |
<term>Scheme</term> |
167 |
<term>Scheme</term> |
| 193 |
|
168 |
|
| 194 |
<listitem> |
169 |
<listitem> |
| 195 |
<para>A dialect of Lisp that is rather more compact and |
170 |
<para>Scheme ¬O LISP ªº¥t¤@¤À¤ä¡AScheme ªº¯SÂI´N¬O¤ñ Common LISP ÁÙn²¼ä¦³¤O¡C |
| 196 |
cleaner than Common Lisp. Popular in Universities as it |
171 |
¥Ñ©ó Scheme ²³æ¡A©Ò¥H«Ü¦h¤j¾Ç®³¨Ó·í§@²Ä¤@°óµ{¦¡»y¨¥±Ð¾Ç±Ð§÷¡C |
| 197 |
is simple enough to teach to undergraduates as a first |
172 |
¦Ó¥B¹ï©ó¬ã¨s¤Hû¨Ó»¡¤]¥i¥H§Ö³tªº¶}µo¥L̩һÝnªºµ{¦¡¡C |
| 198 |
language, while it has a high enough level of |
173 |
</para> |
| 199 |
abstraction to be used in research work.</para> |
174 |
|
| 200 |
|
175 |
<para>Scheme ¦¬¿ý¦b <filename role="package">lang/elk</filename>¡A |
| 201 |
<para>Scheme is available from the Ports Collection as |
176 |
Elk Scheme ª½Ä¶¾¹(¥Ñ³Â¬Ù²z¤u¾Ç°|©Òµo®iªº Scheme ª½Ä¶¾¹)¦¬¿ý¦b |
| 202 |
<filename role="package">lang/elk</filename> for the |
177 |
<filename role="package">lang/mit-scheme</filename>¡A |
| 203 |
Elk Scheme Interpreter. The MIT Scheme Interpreter |
178 |
SCM Scheme Interpreter ¦¬¿ý¦b <filename role="package">lang/scm</filename>¡C</para> |
| 204 |
can be found in |
|
|
| 205 |
<filename role="package">lang/mit-scheme</filename> |
| 206 |
and the SCM Scheme Interpreter in |
| 207 |
<filename role="package">lang/scm</filename>.</para> |
| 208 |
</listitem> |
179 |
</listitem> |
| 209 |
</varlistentry> |
180 |
</varlistentry> |
| 210 |
|
181 |
|
|
Lines 212-222
Link Here
|
| 212 |
<term>Icon</term> |
183 |
<term>Icon</term> |
| 213 |
|
184 |
|
| 214 |
<listitem> |
185 |
<listitem> |
| 215 |
<para>Icon is a high-level language with extensive |
186 |
<para>Icon Äݰª¶¥µ{¦¡»y¨¥¡AIcon ¨ã¦³±j¤jªº¦r¦ê(String)©Mµ²ºc(Structure)³B²z¯à¤O¡C |
| 216 |
facilities for processing strings and structures. |
187 |
&os; Ports Collection ©Ò¦¬¿ýªº Icon ª½Ä¶¾¹ª©¥»«h¬O©ñ¦b |
| 217 |
The version of Icon for &os; can be found in the |
188 |
<filename role="package">lang/icon</filename>¡C</para> |
| 218 |
Ports Collection as |
|
|
| 219 |
<filename role="package">lang/icon</filename>.</para> |
| 220 |
</listitem> |
189 |
</listitem> |
| 221 |
</varlistentry> |
190 |
</varlistentry> |
| 222 |
|
191 |
|
|
Lines 224-239
Link Here
|
| 224 |
<term>Logo</term> |
193 |
<term>Logo</term> |
| 225 |
|
194 |
|
| 226 |
<listitem> |
195 |
<listitem> |
| 227 |
<para>Logo is a language that is easy to learn, and has |
196 |
<para>Logo ¬OºØ®e©ö¾Ç²ßªºµ{¦¡»y¨¥¡A³Ì±`¦b¤@¨Ç±Ð¾Ç½Òµ{¤¤³Q®³¨Ó·í§@¶}ÀY½d¨Ò¡C |
| 228 |
been used as an introductory programming language in |
197 |
¦pªGnµ¹¤pªB¤Í¶}©l¤Wµ{¦¡»y¨¥½Òªº¸Ü¡ALogo ¬O¬Û·í¤£¿ùªº¿ï¾Ü¡C |
| 229 |
various courses. It is an excellent tool to work with |
198 |
¦]¬°¡A§Y¨Ï¹ï¤pªB¤Í¨Ó»¡¡An¥Î Logo ¨Ó¨q¥X½ÆÂø¦hÃä§Î¹Ï§Î¬O¬Û·í»´ÃP®e©öªº¡C</para> |
| 230 |
when teaching programming in small ages, as it makes the |
199 |
|
| 231 |
creation of elaborate geometric shapes an easy task even |
200 |
<para>Logo ¦b &os; Ports Collection ªº³Ì·sª©«h¬O©ñ¦b <filename role="package">lang/logo</filename>¡C</para> |
| 232 |
for very small children.</para> |
|
|
| 233 |
|
| 234 |
<para>The lastest version of Logo for &os; is available from |
| 235 |
the Ports Collection in |
| 236 |
<filename role="package">lang/logo</filename>.</para> |
| 237 |
</listitem> |
201 |
</listitem> |
| 238 |
</varlistentry> |
202 |
</varlistentry> |
| 239 |
|
203 |
|
|
Lines 241-257
Link Here
|
| 241 |
<term>Python</term> |
205 |
<term>Python</term> |
| 242 |
|
206 |
|
| 243 |
<listitem> |
207 |
<listitem> |
| 244 |
<para>Python is an Object-Oriented, interpreted language. |
208 |
<para>Python ¬Oª«¥ó¾É¦Vªºª½Ä¶¦¡»y¨¥¡A |
| 245 |
Its advocates argue that it is one of the best languages |
209 |
Python ªº¾ÖÅ@ªÌÁ`¬O«ÅºÙ Python ¬O³Ì¦n¤Jªùªºµ{¦¡»y¨¥¡C |
| 246 |
to start programming with, since it is relatively easy |
210 |
ÁöµM Python ¥i¥H«Ü²³æªº¶}©l¡A¦ý¬O¤£¥Nªí¥¦´N·|¿éµ¹¨ä¥Lª½Ä¶¦¡»y¨¥(¹³¬O Perl ©M Tcl)¡A |
| 247 |
to start with, but is not limited in comparison to other |
211 |
¨Æ¹êÃÒ©ú Python ¤]¥i¥H®³¨Ó¶}µo¤j«¬¡B½ÆÂøªºÀ³¥Îµ{¦¡¡C |
| 248 |
popular interpreted languages that are used for the |
212 |
</para> |
| 249 |
development of large, complex applications (Perl and |
213 |
|
| 250 |
Tcl are two other languages that are popular for such tasks).</para> |
214 |
<para>&os; Ports Collection ¦¬¿ý¦b <filename role="package">lang/python</filename>¡C</para> |
| 251 |
|
|
|
| 252 |
<para>The latest version of Python is available from the |
| 253 |
Ports Collection in |
| 254 |
<filename role="package">lang/python</filename>.</para> |
| 255 |
</listitem> |
215 |
</listitem> |
| 256 |
</varlistentry> |
216 |
</varlistentry> |
| 257 |
|
217 |
|
|
Lines 259-272
Link Here
|
| 259 |
<term>Ruby</term> |
219 |
<term>Ruby</term> |
| 260 |
|
220 |
|
| 261 |
<listitem> |
221 |
<listitem> |
| 262 |
<para>Ruby is an interpreter, pure object-oriented programming |
222 |
<para>Ruby ¬O¯Âª«¥ó¾É¦Vªºª½Ä¶¦¡»y¨¥¡C |
| 263 |
language. It has become widely popular because of its easy |
223 |
Ruby ¥Ø«e«D±`¬y¦æ¡Aì¦]¦b©ó¥L©öÀ´ªºµ{¦¡»yªkµ²ºc¡A¦b¼¶¼gµ{¦¡®Éªº¼u©Ê¡A |
| 264 |
to understand syntax, flexibility when writing code, and the |
224 |
¥H¤Î¤Ñ¥Í¨ã¦³»´©öªºµo®iºûÅ@¤j«¬±M®×ªº¯à¤O¡C</para> |
| 265 |
ability to easily develop and maintain large, complex |
|
|
| 266 |
programs.</para> |
| 267 |
|
225 |
|
| 268 |
<para>Ruby is available from the Ports Collection as |
226 |
<para>&os; Ports Collection ¦¬¿ý¦b <filename role="package">lang/ruby8</filename>¡C</para> |
| 269 |
<filename role="package">lang/ruby18</filename>.</para> |
|
|
| 270 |
</listitem> |
227 |
</listitem> |
| 271 |
</varlistentry> |
228 |
</varlistentry> |
| 272 |
|
229 |
|
|
Lines 274-343
Link Here
|
| 274 |
<term>Tcl and Tk</term> |
231 |
<term>Tcl and Tk</term> |
| 275 |
|
232 |
|
| 276 |
<listitem> |
233 |
<listitem> |
| 277 |
<para>Tcl is an embeddable, interpreted language, that has |
234 |
<para>Tcl ¬O¤º´O¦¡ªºª½Ä¶¦¡»y¨¥¡AÅý Tcl ¥i¥H¦p¦¹¼sªx¹B¥Îªºì¦]¬O Tcl ªº²¾´Ó©Ê¡C |
| 278 |
become widely used and became popular mostly because of its portability to many |
235 |
Tcl ¤]¥i¥H§Ö³tµo®i¤@Ó²³æ¦ý¬O¨ã¦³Âú«¬ªºµ{¦¡©ÎªÌ¨ã¦³§¹¾ã¥\¯àªºµ{¦¡¡C</para> |
| 279 |
platforms. It can be used both for quickly writing |
236 |
|
| 280 |
small, prototype applications, or (when combined with |
237 |
<para>Tcl ³\¦hªºª©¥»³£¥i¦b &os; ¤W¹B§@¡A¦Ó³Ì·sªº Tcl ª©¥»¬° Tcl 8.4¡A |
| 281 |
Tk, a GUI toolkit) fully-fledged, featureful |
238 |
&os; Ports Collection ¦¬¿ý¦b <filename role="package">lang/tcl84</filename>¡C</para> |
| 282 |
programs.</para> |
|
|
| 283 |
|
| 284 |
<para>Various versions of Tcl are available as ports |
| 285 |
for &os;. The latest version, Tcl 8.4, can be found in |
| 286 |
<filename role="package">lang/tcl84</filename>.</para> |
| 287 |
</listitem> |
239 |
</listitem> |
| 288 |
</varlistentry> |
240 |
</varlistentry> |
| 289 |
</variablelist> |
241 |
</variablelist> |
| 290 |
</sect2> |
242 |
</sect2> |
| 291 |
|
243 |
|
| 292 |
<sect2> |
244 |
<sect2> |
| 293 |
<title>Compilers</title> |
245 |
<title>½s;¹</title> |
| 294 |
|
|
|
| 295 |
<para>Compilers are rather different. First of all, you write |
| 296 |
your code in a file (or files) using an editor. You then run |
| 297 |
the compiler and see if it accepts your program. If it did |
| 298 |
not compile, grit your teeth and go back to the editor; if it |
| 299 |
did compile and gave you a program, you can run it either at a |
| 300 |
shell command prompt or in a debugger to see if it works |
| 301 |
properly. |
| 302 |
|
246 |
|
|
|
247 |
<para>½s;¹©Mª½Ä¶¾¹¨âªÌ¬Û¤ñªº¸Ü¡A¦³¨Ç¤£¦P¡Aº¥ý´N¬O¥²¶·¥ý§âµ{¦¡½X²Î²Î¼g¤J¨ìÀɮ׸̱¡A |
| 248 |
µM«á¥²¶·°õ¦æ½s;¹¨Ó¸ÕµÛ½s͵{¦¡¡A¦pªG½s;¹¤£±µ¨ü©Ò¼gªºµ{¦¡¡A¨º´N¥²¶·¤@ª½×§ïµ{¦¡¡A |
| 249 |
ª½¨ì½s;¹±µ¨ü¥B§â§Aªºµ{¦¡½sͦ¨°õ¦æÀÉ¡C |
| 250 |
¦¹¥~¡A¤]¥i¥H¦b´£¥Ü©R¥O¦C¡A©Î¦b°£¿ù¾¹¤¤°õ¦æ§A½sͦnªºµ{¦¡¬Ý¬Ý¥¦¬O§_¥i¥H¹B§@¡C |
| 303 |
<footnote> |
251 |
<footnote> |
| 304 |
<para>If you run it in the shell, you may get a core |
252 |
<para>¦pªG¦b´£¥Ü©R¥O¦C¤U°õ¦æ¡A¨º»ò¦³¥i¯à·|²£¥Í core dump¡C</para> |
| 305 |
dump.</para> |
|
|
| 306 |
</footnote></para> |
253 |
</footnote></para> |
| 307 |
|
254 |
|
| 308 |
<para>Obviously, this is not quite as direct as using an |
255 |
<para>«Ü©úÅ㪺¡A¨Ï¥Î½s;¹¨Ã¤£¹³ª½Ä¶¾¹¯ë¥i¥H°¨¤W±o¨ìµ²ªG¡C |
| 309 |
interpreter. However it allows you to do a lot of things |
256 |
¤£ºÞ¦p¦ó¡A½s;¹¤¹³\§A§@«Ü¦hª½Ä¶¾¹¤£¥i¯à©ÎªÌ¬O«ÜÃø¹F¨ìªº¨Æ±¡¡C |
| 310 |
which are very difficult or even impossible with an |
257 |
¨Ò¦p¡G¼¶¼g©M§@·~¨t²Î±K¤Á¤¬°Êªºµ{¦¡¡A¬Æ¦Ü¬O§A¦Û¤v¼gªº§@·~¨t²Î¡I |
| 311 |
interpreter, such as writing code which interacts closely with |
258 |
·í§A·Qn¼g¥X°ª®Ä²vªºµ{¦¡®É¡A½s;¹«K¬£¤W¥Î³õ¤F¡C |
| 312 |
the operating system—or even writing your own operating |
259 |
½s;¹¥i¥H¦b½sͮɶ¶«K³Ì¨Î¤Æ§Aªºµ{¦¡¡A¦ý¬Oª½Ä¶¾¹«o¤£¦æ¡C |
| 313 |
system! It is also useful if you need to write very efficient |
260 |
¦Ó½s;¹»Pª½Ä¶¾¹³Ì¤jªº®t§O¦b©ó¡G·í§A·Q§â§A¼g¦nªºµ{¦¡®³¨ì¥t¥~¤@¥x¾÷¾¹¤W¶]®É¡A |
| 314 |
code, as the compiler can take its time and optimize the code, |
261 |
§A¥un±N½s;¹½sĶ¥X¨Óªº¥i°õ¦æÀÉ¡A®³¨ì·s¾÷¾¹¤W«K¥i¥H°õ¦æ¡A |
| 315 |
which would not be acceptable in an interpreter. Moreover, |
262 |
¦Óª½Ä¶¾¹«h¥²¶·n¨D·s¾÷¾¹¤W¡A¥²¶·n¦³¸ò¥t¤@¥x¾÷¾¹¤W¬Û¦Pªºª½Ä¶¾¹¡A |
| 316 |
distributing a program written for a compiler is usually more |
263 |
¤~¯à²ÕͰõ¦æ§Aªºµ{¦¡¡I |
| 317 |
straightforward than one written for an interpreter—you |
264 |
</para> |
| 318 |
can just give them a copy of the executable, assuming they |
265 |
|
| 319 |
have the same operating system as you.</para> |
266 |
<para>½sͦ¡ªºµ{¦¡»y¨¥¥]§t Pascal¡BC ©M C++¡A |
| 320 |
|
267 |
C ©M C++ ¤£¬O¤@Ó¿Ë©M¤O¤Q¨¬ªº»y¨¥¡A¦ý¬O«Ü¾A¦X¨ã¦³¸gÅ窺 Programmer¡C |
| 321 |
<para>Compiled languages include Pascal, C and C++. C and C++ |
268 |
Pascal ¨ä¹ê¬O¤@Ó³]p¥Î¨Ó±Ð¾Ç¥Îªºµ{¦¡»y¨¥¡A¦Ó¥B¤]«Ü¾A¦X¥Î¨Ó¤Jªù¡A |
| 322 |
are rather unforgiving languages, and best suited to more |
269 |
&os; ¹w³]¨Ã¨S¦³§â Pascal ¾ã¦X¶i base system ¤¤¡A |
| 323 |
experienced programmers; Pascal, on the other hand, was |
270 |
¦ý¬O GNU Pascal Compiler ©M Free Pascal Compiler ³£¥i¤À§O¦b |
| 324 |
designed as an educational language, and is quite a good |
271 |
<filename role="package">lang/gpc</filename> ©M <filename role="package">lang/fpc</filename> ¤¤§ä¨ì¡C</para> |
| 325 |
language to start with. FreeBSD does not include Pascal |
272 |
|
| 326 |
support in the base system, but both GNU Pascal Compiler (GPC) |
273 |
<para>¦pªG§A¥Î¤£¦Pªºµ{¦¡¨Ó¼g½sͦ¡µ{¦¡¡A¨º»ò¤£Â_¦a½s¿è-½sĶ-°õ¦æ-°£¿ùªº³oÓ´`ÀôªÖ©w·|«Ü·Ð¤H¡A |
| 327 |
and the Free Pascal Compiler |
274 |
¬°¤F§ó²¤Æ¡B¤è«Kµ{¦¡¶}µo¬yµ{¡A«Ü¦h°Ó·~½s;¹¼t°Ó¶}©lµo®i©Ò¿×ªº <acronym>IDE</acronym>(Integrated Development Environments) ¶}µoÀô¹Ò¡A |
| 328 |
are available in the ports collection as |
275 |
FreeBSD ¹w³]¨Ã¨S¦³§â IDE ¾ã¦X¶i base system ¤¤¡A¦ý¬O§A¥i³z¹L <filename role="package">devel/kdevelop</filename> ¦w¸Ë kdevelop |
| 329 |
<filename role="package">lang/gpc</filename> and |
276 |
©Î¨Ï¥Î <application>Emacs</application> ¨ÓÅéÅç IDE ¶}µoÀô¹Ò¡C |
| 330 |
<filename role="package">lang/fpc</filename>.</para> |
277 |
¦b«á±ªº <xref linkend="emacs"> ±MÃD±N¤¶²Ð¡A¦p¦ó¥H <application>Emacs</application> ¨Ó§@¬° IDE ¶}µoÀô¹Ò¡C</para> |
| 331 |
|
|
|
| 332 |
<para>As the edit-compile-run-debug cycle is rather tedious when |
| 333 |
using separate programs, many commercial compiler makers have |
| 334 |
produced Integrated Development Environments |
| 335 |
(<acronym>IDE</acronym>s for short). FreeBSD does not include |
| 336 |
an IDE in the base system, but <filename role="package">devel/kdevelop</filename> is |
| 337 |
available in the ports tree and many use |
| 338 |
<application>Emacs</application> for this purpose. Using |
| 339 |
<application>Emacs</application> as an IDE is discussed in |
| 340 |
<xref linkend="emacs">.</para> |
| 341 |
</sect2> |
278 |
</sect2> |
| 342 |
|
279 |
|
| 343 |
|
280 |
|
|
Lines 345-471
Link Here
|
| 345 |
|
282 |
|
| 346 |
|
283 |
|
| 347 |
<sect1 id="tools-compiling"> |
284 |
<sect1 id="tools-compiling"> |
| 348 |
<title>Compiling with <command>cc</command></title> |
285 |
<title>¥Î <command>cc</command> ¨Ó½s͵{¦¡</title> |
|
|
286 |
|
| 287 |
<para>¥»³¹½d¨Ò¥u¦³°w¹ï GNU C compiler ©M GNU C++ compiler §@»¡©ú¡A |
| 288 |
³o¨âÓ¦b FreeBSD base system ¤¤´N¦³¤F¡A |
| 289 |
ª½±µ¥´ <command>cc</command> ©Î <command>gcc</command> ´N¥i¥H°õ¦æ¡C |
| 290 |
¦Ü©ó¡A¦p¦ó¥Îª½Ä¶¾¹²£¥Íµ{¦¡ªº»¡©ú¡A³q±`¥i¦bª½Ä¶¾¹ªº¤å¥ó©Î½u¤W¤å¥ó§ä¨ì»¡©ú¡A¦]¦¹¤£¦AÂØz¡C</para> |
| 349 |
|
291 |
|
| 350 |
<para>This section deals only with the GNU compiler for C and C++, |
292 |
<para>·í§A¼g§¹§Aªº³Ç§@«á¡A±µ¤U¨Ó«K¬OÅý³oÓµ{¦¡¥i¥H¦b FreeBSD ¤W°õ¦æ¡A |
| 351 |
since that comes with the base FreeBSD system. It can be |
293 |
³q±`³o¨Çn¤@¨Ç¨BÆJ¤~¯à§¹¦¨¡A¦³¨Ç¨BÆJ«h»Ýn¤£¦Pµ{¦¡¨Ó§¹¦¨¡C</para> |
| 352 |
invoked by either <command>cc</command> or <command>gcc</command>. The |
|
|
| 353 |
details of producing a program with an interpreter vary |
| 354 |
considerably between interpreters, and are usually well covered |
| 355 |
in the documentation and on-line help for the |
| 356 |
interpreter.</para> |
| 357 |
|
| 358 |
<para>Once you have written your masterpiece, the next step is to |
| 359 |
convert it into something that will (hopefully!) run on FreeBSD. |
| 360 |
This usually involves several steps, each of which is done by a |
| 361 |
separate program.</para> |
| 362 |
|
294 |
|
| 363 |
<procedure> |
295 |
<procedure> |
| 364 |
<step> |
296 |
<step> |
| 365 |
<para>Pre-process your source code to remove comments and do |
297 |
<para>¹w¥ý³B²z(Pre-process)§Aªºµ{¦¡½X¡A²¾°£µ{¦¡¤ºªºµù¸Ñ¡A©M¨ä¥L§Þ¥©¡A |
| 366 |
other tricks like expanding macros in C.</para> |
298 |
¹³¬O expanding(ÂX¤j) C ªº marco¡C</para> |
| 367 |
</step> |
299 |
</step> |
| 368 |
|
300 |
|
| 369 |
<step> |
301 |
<step> |
| 370 |
<para>Check the syntax of your code to see if you have obeyed |
302 |
<para>½T»{§Aªºµ{¦¡»yªk¬O§_½T¹ê¿í·Ó C/C++ ªº³W©w¡A¦pªG¨S¦³²Å¦Xªº¸Ü¡A½s;¹·|¥X²{ĵ§i¡C</para> |
| 371 |
the rules of the language. If you have not, it will |
|
|
| 372 |
complain!</para> |
| 373 |
</step> |
303 |
</step> |
| 374 |
|
304 |
|
| 375 |
<step> |
305 |
<step> |
| 376 |
<para>Convert the source code into assembly |
306 |
<para>±Nì©l½XÂন²Õ¦X»y¨¥ — ¥¦¸ò¾÷¾¹»y¨¥(machine code)«D±`¬Ûªñ¡A¦ý¤´¦b¤HÃþ¥i²z¸Ñªº½d³ò¤º(¾Ú»¡À³¸Ó¬O³o¼Ë)¡C |
| 377 |
language—this is very close to machine code, but still |
|
|
| 378 |
understandable by humans. Allegedly. |
| 379 |
|
| 380 |
<footnote> |
307 |
<footnote> |
| 381 |
<para>To be strictly accurate, <command>cc</command> converts the |
308 |
<para>ÄY®æ»¡°_¨Ó¡A¦b³oÓ¶¥¬q <command>cc</command> ¨Ã¤£¬O¯uªº§âì©lµ{¦¡Âন²Õ¦X»y¨¥¡A |
| 382 |
source code into its own, machine-independent |
309 |
¦Ó¬OÂର machine-independent ªº <firstterm>p-code</firstterm>¡C</para> |
| 383 |
<firstterm>p-code</firstterm> instead of assembly language at |
|
|
| 384 |
this stage.</para> |
| 385 |
</footnote></para> |
310 |
</footnote></para> |
| 386 |
</step> |
311 |
</step> |
| 387 |
|
312 |
|
| 388 |
<step> |
313 |
<step> |
| 389 |
<para>Convert the assembly language into machine |
314 |
<para>§â²Õ¦X»y¨¥Âন¾÷¾¹»y¨¥ — ¬Oªº¡A³o¸Ì»¡ªº¾÷¾¹»y¨¥´N¬O±`´£¨ìªº bit ©M byte¡A¤]´N¬O 1 ©M 0¡C</para> |
| 390 |
code—yep, we are talking bits and bytes, ones and |
|
|
| 391 |
zeros here.</para> |
| 392 |
</step> |
315 |
</step> |
| 393 |
|
316 |
|
| 394 |
<step> |
317 |
<step> |
| 395 |
<para>Check that you have used things like functions and |
318 |
<para>½T»{µ{¦¡¤¤¥Î¨ìªº¨ç¦¡©I¥s¡B¥þ°ìÅܼƬO§_¥¿½T¡AÁ|¨Ò¨Ó»¡¡G¦pY©I¥s¤F¤£¦s¦bªº¨ç¦¡¡A½s;¹·|Åã¥Üĵ§i¡C</para> |
| 396 |
global variables in a consistent way. For example, if you |
|
|
| 397 |
have called a non-existent function, it will |
| 398 |
complain.</para> |
| 399 |
</step> |
319 |
</step> |
| 400 |
|
320 |
|
| 401 |
<step> |
321 |
<step> |
| 402 |
<para>If you are trying to produce an executable from several |
322 |
<para>¦pªGµ{¦¡¬O¥Ñµ{¦¡½XÀɮרӽsĶ¡A½s;¹·|¾ã¦X°_¨Ó¡C</para> |
| 403 |
source code files, work out how to fit them all |
|
|
| 404 |
together.</para> |
| 405 |
</step> |
323 |
</step> |
| 406 |
|
324 |
|
| 407 |
<step> |
325 |
<step> |
| 408 |
<para>Work out how to produce something that the system's |
326 |
<para>½s;¹·|t³d²£¥ÍªF¦è¡AÅý¨t²Î¤Wªº run-time loader ¥i¥H§âµ{¦¡¸ü¤J°O¾ÐÅ餺°õ¦æ¡C</para> |
| 409 |
run-time loader will be able to load into memory and |
|
|
| 410 |
run.</para> |
| 411 |
</step> |
327 |
</step> |
| 412 |
|
328 |
|
| 413 |
<step> |
329 |
<step> |
| 414 |
<para>Finally, write the executable on the filesystem.</para> |
330 |
<para>³Ì«á·|§â½sͧ¹ªº°õ¦æÀɦs¦bµwºÐ¤W¡C</para> |
| 415 |
</step> |
331 |
</step> |
| 416 |
</procedure> |
332 |
</procedure> |
| 417 |
|
333 |
|
| 418 |
<para>The word <firstterm>compiling</firstterm> is often used to refer to |
334 |
<para>³q±` <firstterm>½sĶ(compiling)</firstterm> ¬O«ü²Ä 1 ¨ì²Ä 4 Ó¨BÆJ¡C |
| 419 |
just steps 1 to 4—the others are referred to as |
335 |
— ¨ä¥L¨BÆJ«hºÙ¬° <firstterm>³sµ²(linking)</firstterm>¡A |
| 420 |
<firstterm>linking</firstterm>. Sometimes step 1 is referred to as |
336 |
¦³®ÉÔ¨BÆJ 1 ¤]¥i¥H¬O«ü <firstterm>¹w¥ý³B²z(pre-processing)</firstterm>¡A |
| 421 |
<firstterm>pre-processing</firstterm> and steps 3-4 as |
337 |
¦Ó¨BÆJ 3 ¨ì¨BÆJ 4 «h¬O <firstterm>²ÕĶ(assembling)</firstterm>¡C</para> |
| 422 |
<firstterm>assembling</firstterm>.</para> |
338 |
|
| 423 |
|
339 |
<para>©¯¹Bªº¬O¡A§A¥i¥H¤£¥Î²z·|¥H¤W²Ó¸`¡A½s;¹³£·|¦Û°Ê§¹¦¨¡C |
| 424 |
<para>Fortunately, almost all this detail is hidden from you, as |
340 |
¦]¬° <command>cc</command> ¥u¬O¬OÓ«eºÝµ{¦¡(front end)¡A¥¦·|¨Ì·Ó¥¿½Tªº°Ñ¼Æ¨Ó©I¥s¬ÛÃöµ{¦¡À°§A³B²z¡C |
| 425 |
<command>cc</command> is a front end that manages calling all these |
341 |
¥u»Ý¥´¡G |
| 426 |
programs with the right arguments for you; simply typing</para> |
|
|
| 427 |
|
| 428 |
<screen>&prompt.user; <userinput>cc foobar.c</userinput></screen> |
342 |
<screen>&prompt.user; <userinput>cc foobar.c</userinput></screen> |
| 429 |
|
343 |
|
| 430 |
<para>will cause <filename>foobar.c</filename> to be compiled by all the |
344 |
<para>¤Wz«ü¥O·|§â <filename>foobar.c</filename> ¶}©l½sĶ¡A¨Ã§¹¦¨¤Wz°Ê§@¡C |
| 431 |
steps above. If you have more than one file to compile, just do |
345 |
¦pªG§A¦³³\¦hÀÉ®×»Ýn½sĶ¡A¨º½Ð¥´Ãþ¦ü¤U¦C«ü¥O§Y¥i¡G</para> |
| 432 |
something like</para> |
|
|
| 433 |
|
346 |
|
| 434 |
<screen>&prompt.user; <userinput>cc foo.c bar.c</userinput></screen> |
347 |
<screen>&prompt.user; <userinput>cc foo.c bar.c</userinput></screen> |
| 435 |
|
348 |
|
| 436 |
<para>Note that the syntax checking is just that—checking |
349 |
<para>°O¦í»yªk¿ù»~Àˬd´N¬O — ¯ÂºéÀˬd»yªk¿ù»~»P§_¡A |
| 437 |
the syntax. It will not check for any logical mistakes you may |
350 |
¦Ó¤£·|À°§AÀË´ú¥ô¦óÅÞ¿è¿ù»~¡A¤ñ¦p¡GµL°j°é¡A©Î¬O±Æ§Ç¤è¦¡·Q¥Î binary sort «o§Ë¦¨ bubble sort¡C |
| 438 |
have made, like putting the program into an infinite loop, or |
|
|
| 439 |
using a bubble sort when you meant to use a binary |
| 440 |
sort. |
| 441 |
|
| 442 |
<footnote> |
351 |
<footnote> |
| 443 |
<para>In case you did not know, a binary sort is an efficient |
352 |
<para>è©Ò»¡ªº binary sort ©M bubble sort °ÝÃD¡A |
| 444 |
way of sorting things into order and a bubble sort |
353 |
¦b¤w±Æ§Ç¦nªº§Ç¦C¤¤¡Abinary sort ·j¯Á®Ä²v·|¤ñ bubble sort ¦n¡C</para> |
| 445 |
is not.</para> |
|
|
| 446 |
</footnote></para> |
354 |
</footnote></para> |
| 447 |
|
355 |
|
| 448 |
<para>There are lots and lots of options for <command>cc</command>, which |
356 |
<para><command>cc</command> ¦³«D±`¦hªº¿ï¶µ¡A³£¥i³z¹L½u¤W¤â¥U¨Ó¬d¡C |
| 449 |
are all in the manual page. Here are a few of the most important |
357 |
¤U±¥u´£¤@¨Ç¥²n¥B«nªº¿ï¶µ¡A¥H§@¬°¨Ò¤l¡C</para> |
| 450 |
ones, with examples of how to use them.</para> |
|
|
| 451 |
|
358 |
|
| 452 |
<variablelist> |
359 |
<variablelist> |
| 453 |
<varlistentry> |
360 |
<varlistentry> |
| 454 |
<term><option>-o <replaceable>filename</replaceable></option></term> |
361 |
<term><option>-o <replaceable>ÀɦW</replaceable></option></term> |
| 455 |
|
362 |
|
| 456 |
<listitem> |
363 |
<listitem> |
| 457 |
<para>The output name of the file. If you do not use this |
364 |
<para><option>-o</option> ½sĶ«áªº°õ¦æÀÉÀɦW¡A¦pªG¨S¦³¨Ï¥Î³o¿ï¶µªº¸Ü¡A |
| 458 |
option, <command>cc</command> will produce an executable called |
365 |
½sͦnªºµ{¦¡¹w³]ÀɦW±N·|¬O <filename>a.out</filename> |
| 459 |
<filename>a.out</filename>. |
|
|
| 460 |
|
366 |
|
| 461 |
<footnote> |
367 |
<footnote> |
| 462 |
<para>The reasons for this are buried in the mists of |
368 |
<para>¦Ü©ó <option>-o</option> ªºì¦]¡A«h¬O¤@¹Î¾ú¥v°gÃú¤F¡C</para> |
| 463 |
history.</para> |
|
|
| 464 |
</footnote></para> |
369 |
</footnote></para> |
| 465 |
|
370 |
|
| 466 |
<informalexample> |
371 |
<informalexample> |
| 467 |
<screen>&prompt.user; <userinput>cc foobar.c</userinput> <lineannotation>executable is <filename>a.out</filename></lineannotation> |
372 |
<screen>&prompt.user; <userinput>cc foobar.c</userinput> <lineannotation>°õ¦æÀÉ´N¬O <filename>a.out</filename></lineannotation> |
| 468 |
&prompt.user; <userinput>cc -o foobar foobar.c</userinput> <lineannotation>executable is <filename>foobar</filename></lineannotation> |
373 |
&prompt.user; <userinput>cc -o foobar foobar.c</userinput> <lineannotation>°õ¦æÀÉ´N¬O <filename>foobar</filename></lineannotation> |
| 469 |
</screen> |
374 |
</screen> |
| 470 |
</informalexample> |
375 |
</informalexample> |
| 471 |
</listitem> |
376 |
</listitem> |
|
Lines 475-493
Link Here
|
| 475 |
<term><option>-c</option></term> |
380 |
<term><option>-c</option></term> |
| 476 |
|
381 |
|
| 477 |
<listitem> |
382 |
<listitem> |
| 478 |
<para>Just compile the file, do not link it. Useful for toy |
383 |
<para>¨Ï¥Î <option>-c</option> ®É¡A¥u·|½sĶì©l½X¡A¦Ó¤£§@³sµ²(linking)¡C |
| 479 |
programs where you just want to check the syntax, or if |
384 |
·í¥u·Q½T»{»yªk¬O§_¥¿½T©Î¨Ï¥Î Makefile ¨Ó½s͵{¦¡®É¡A³oÓ¿ï¶µ«D±`¦³¥Î¡C</para> |
| 480 |
you are using a <filename>Makefile</filename>.</para> |
|
|
| 481 |
|
385 |
|
| 482 |
<informalexample> |
386 |
<informalexample> |
| 483 |
<screen>&prompt.user; <userinput>cc -c foobar.c</userinput> |
387 |
<screen> |
|
|
388 |
&prompt.user; <userinput>cc -c foobar.c</userinput> |
| 484 |
</screen> |
389 |
</screen> |
| 485 |
</informalexample> |
390 |
</informalexample> |
| 486 |
|
391 |
|
| 487 |
<para>This will produce an <firstterm>object file</firstterm> (not an |
392 |
<para>³o·|²£¥Í¥s°µ <filename>foobar</filename> ªº <firstterm>object file</firstterm>(«D°õ¦æÀÉ)¡C |
| 488 |
executable) called <filename>foobar.o</filename>. This |
393 |
³oÀÉ¥i¥H»P¨ä¥Lªº object file ³sµ²¦b¤@°_¡A¦Ó¦¨°õ¦æÀÉ¡C</para> |
| 489 |
can be linked together with other object files into an |
|
|
| 490 |
executable.</para> |
| 491 |
</listitem> |
394 |
</listitem> |
| 492 |
</varlistentry> |
395 |
</varlistentry> |
| 493 |
|
396 |
|
|
Lines 495-527
Link Here
|
| 495 |
<term><option>-g</option></term> |
398 |
<term><option>-g</option></term> |
| 496 |
|
399 |
|
| 497 |
<listitem> |
400 |
<listitem> |
| 498 |
<para>Create a debug version of the executable. This makes |
401 |
<para> |
| 499 |
the compiler put information into the executable about |
402 |
<option>-g</option> ±N·|§â¤@¨Çµ¹ gdb ¥Îªº°£¿ù°T®§¥]¶i¥h°õ¦æÀɸ̱¡A©Ò¿×ªº°£¿ù°T®§¨Ò¦p¡G |
| 500 |
which line of which source file corresponds to which |
403 |
µ{¦¡¦b²Ä´X¦æ¥X¿ù¡B¨ºÓµ{¦¡²Ä´X¦æ°µ¤°»ò¨ç¦¡©I¥sµ¥µ¥¡C°£¿ù¸ê°T<emphasis>«D±`</emphasis>¦n¥Î¡C |
| 501 |
function call. A debugger can use this information to show |
404 |
¦ý¯ÊÂI´N¬O¡G¹ï©óµ{¦¡¨Ó»¡¡AÃB¥~ªº°£¿ù°T®§·|Åý½sĶ¥X¨Óªºµ{¦¡¤ñ¸ûªÎ¨Ç¡C |
| 502 |
the source code as you step through the program, which is |
405 |
<option>-g</option> ªº¾A¥Î®É¾÷¦b©ó¡G·íµ{¦¡ÁÙ¦b¶}µo®É¨Ï¥Î´N¦n¡A |
| 503 |
<emphasis>very</emphasis> useful; the disadvantage is that |
406 |
¦Ó·í§AnÄÀ¥X§Aªº <quote>µo¦æª©¥»(release version)</quote> |
| 504 |
all this extra information makes the program much bigger. |
407 |
©ÎªÌ½T»{µ{¦¡¥i¹B§@¥¿±`ªº¸Ü¡A´N¤£¥²¥Î <option>-g</option> ³o¿ï¶µ¤F¡C</para> |
| 505 |
Normally, you compile with <option>-g</option> while you |
|
|
| 506 |
are developing a program and then compile a <quote>release |
| 507 |
version</quote> without <option>-g</option> when you are |
| 508 |
satisfied it works properly.</para> |
| 509 |
|
408 |
|
| 510 |
<informalexample> |
409 |
<informalexample> |
| 511 |
<screen>&prompt.user; <userinput>cc -g foobar.c</userinput> |
410 |
<screen>&prompt.user; <userinput>cc -g foobar.c</userinput> |
| 512 |
</screen> |
411 |
</screen> |
| 513 |
</informalexample> |
412 |
</informalexample> |
| 514 |
|
413 |
|
| 515 |
<para>This will produce a debug version of the |
414 |
<para>³o°Ê§@·|²£¥Í¦³§t°£¿ù°T®§ªº°õ¦æÀÉ¡C |
| 516 |
program. |
|
|
| 517 |
|
| 518 |
<footnote> |
415 |
<footnote> |
| 519 |
<para>Note, we did not use the <option>-o</option> flag |
416 |
<para>½Ðª`·N¡A¦]¬°¤W¨Ò¨S¥Î <option>-o</option> ¥H«ü©w°õ¦æÀɦWºÙ¡A |
| 520 |
to specify the executable name, so we will get an |
417 |
©Ò¥H°õ¦æÀÉ·|¬O <filename>a.out</filename> ³oÀÉ¡C |
| 521 |
executable called <filename>a.out</filename>. |
418 |
¨º»ò¡An¦p¦ó²£¥Í <filename>foobar</filename> ªº°õ¦æÀɨ产t°£¿ù°T®§¡A |
| 522 |
Producing a debug version called |
419 |
³o´N¯d«Ý¬Ý¾Ì½m²ß¤@¤UÅo¡C</para> |
| 523 |
<filename>foobar</filename> is left as an exercise for |
|
|
| 524 |
the reader!</para> |
| 525 |
</footnote></para> |
420 |
</footnote></para> |
| 526 |
</listitem> |
421 |
</listitem> |
| 527 |
</varlistentry> |
422 |
</varlistentry> |
|
Lines 530-574
Link Here
|
| 530 |
<term><option>-O</option></term> |
425 |
<term><option>-O</option></term> |
| 531 |
|
426 |
|
| 532 |
<listitem> |
427 |
<listitem> |
| 533 |
<para>Create an optimized version of the executable. The |
428 |
<para><option>-O</option> ·|²£¥Í³Ì¨Î¤Æªº°õ¦æÀÉ¡A |
| 534 |
compiler performs various clever tricks to try to produce |
429 |
½s;¹·|¨Ï¥Î¤@¨Ç§Þ¥©¡A¨ÓÅýµ{¦¡¥i¥H¶]ªº¤ñ¥¼¸g³Ì¨Î¤Æªºµ{¦¡ÁÙ§Ö¡A |
| 535 |
an executable that runs faster than normal. You can add a |
430 |
§A¥i¥H¦b¤j¼g O «á±¥[¤W¼Æ¦r¨Ó«ü©ú§A·Qnªº³Ì¨Î¤Æ¼h¯Å¡C |
| 536 |
number after the <option>-O</option> to specify a higher |
431 |
¦ý¬O³Ì¨Î¤ÆÁÙ¬O·|¦³¤@¨Ç¿ù»~¡AÁ|¨Ò¨Ó»¡¦b·í¦b FreeBSD 2.10 release ¤¤¨Ï¥Î <command>cc</command> |
| 537 |
level of optimization, but this often exposes bugs in the |
432 |
¨Ã¥B«ü©w <option>-O2</option> ®É¡A¦b¬Y¨Ç±¡§Î¤U·|²£¥Í¿ù»~ªº°õ¦æÀÉ¡C</para> |
| 538 |
compiler's optimizer. For instance, the version of |
|
|
| 539 |
<command>cc</command> that comes with the 2.1.0 release of |
| 540 |
FreeBSD is known to produce bad code with the |
| 541 |
<option>-O2</option> option in some circumstances.</para> |
| 542 |
|
433 |
|
| 543 |
<para>Optimization is usually only turned on when compiling |
434 |
<para>¥u¦³·ínÄÀ¥Xµo¦æª©¥»¡B©ÎªÌ¥[³tµ{¦¡®É¡A¤~»Ýn¨Ï¥Î³Ì¨Î¤Æ¿ï¶µ¡C |
| 544 |
a release version.</para> |
435 |
</para> |
| 545 |
|
436 |
|
| 546 |
<informalexample> |
437 |
<informalexample> |
| 547 |
<screen>&prompt.user; <userinput>cc -O -o foobar foobar.c</userinput> |
438 |
<screen>&prompt.user; <userinput>cc -O -o foobar foobar.c</userinput> |
| 548 |
</screen> |
439 |
</screen> |
| 549 |
</informalexample> |
440 |
</informalexample> |
| 550 |
|
441 |
|
| 551 |
<para>This will produce an optimized version of |
442 |
<para>³o·|²£¥Í <filename>foobar</filename> °õ¦æÀɪº³Ì¨Î¤Æª©¥»¡C</para> |
| 552 |
<filename>foobar</filename>.</para> |
|
|
| 553 |
</listitem> |
443 |
</listitem> |
| 554 |
</varlistentry> |
444 |
</varlistentry> |
| 555 |
</variablelist> |
445 |
</variablelist> |
| 556 |
|
446 |
|
| 557 |
<para>The following three flags will force <command>cc</command> |
447 |
<para> |
| 558 |
to check that your code complies to the relevant international |
448 |
¥H¤U¤TӰѼƱN·|±j¢ <command>cc</command> ½T»{µ{¦¡½X¬O§_²Å¦X¤@¨Ç°ê»Ú¼Ð·Çªº³W½d¡A |
| 559 |
standard, often referred to as the <acronym>ANSI</acronym> |
449 |
¤]´N¬O³q±`»¡ªº <acronym>ANSI</acronym> ¼Ð·Ç¡A |
| 560 |
standard, though strictly speaking it is an |
450 |
¦Ó <acronym>ANSI</acronym> ÄY®æ¨ÓÁ¿ÄÝ <acronym>ISO</acronym> ¼Ð·Ç¡C |
| 561 |
<acronym>ISO</acronym> standard.</para> |
451 |
</para> |
| 562 |
|
452 |
|
| 563 |
<variablelist> |
453 |
<variablelist> |
| 564 |
<varlistentry> |
454 |
<varlistentry> |
| 565 |
<term><option>-Wall</option></term> |
455 |
<term><option>-Wall</option></term> |
| 566 |
|
456 |
|
| 567 |
<listitem> |
457 |
<listitem> |
| 568 |
<para>Enable all the warnings which the authors of |
458 |
<para><option>-Wall</option> Åã¥Ü <command>cc</command> ºûÅ@ªÌ©Ò»{¬°È±oª`·Nªº©Ò¦³Äµ§i°T®§¡C |
| 569 |
<command>cc</command> believe are worthwhile. Despite the |
459 |
¤£¹L³o¦W¦r¥i¯à·|³y¦¨»~¸Ñ¡A¨Æ¹ê¤W¥¦¨Ã¥¼§¹¥þÅã¥Ü <command>cc</command> ©Ò¯àª`·N¨ìªº¦U¶µÄµ§i°T®§¡C</para> |
| 570 |
name, it will not enable all the warnings |
|
|
| 571 |
<command>cc</command> is capable of.</para> |
| 572 |
</listitem> |
460 |
</listitem> |
| 573 |
</varlistentry> |
461 |
</varlistentry> |
| 574 |
|
462 |
|
|
Lines 576-586
Link Here
|
| 576 |
<term><option>-ansi</option></term> |
464 |
<term><option>-ansi</option></term> |
| 577 |
|
465 |
|
| 578 |
<listitem> |
466 |
<listitem> |
| 579 |
<para>Turn off most, but not all, of the |
467 |
<para><option>-ansi</option> Ãö³¬ <command>cc</command> ¯S¦³ªº¬Y¨Ç¯S®í«D ANSI C ¼Ð·Ç¥\¯à¡C |
| 580 |
non-<acronym>ANSI</acronym> C features provided by |
468 |
¤£¹L³o¦W¦r¥i¯à·|³y¦¨»~¸Ñ¡A¨Æ¹ê¤W¥¦¨Ã¤£«OÃÒ§Aªºµ{¦¡·|§¹¥þ²Å¦X ANSI ¼Ð·Ç¡C</para> |
| 581 |
<command>cc</command>. Despite the name, it does not |
|
|
| 582 |
guarantee strictly that your code will comply to the |
| 583 |
standard.</para> |
| 584 |
</listitem> |
469 |
</listitem> |
| 585 |
</varlistentry> |
470 |
</varlistentry> |
| 586 |
|
471 |
|
|
Lines 588-661
Link Here
|
| 588 |
<term><option>-pedantic</option></term> |
473 |
<term><option>-pedantic</option></term> |
| 589 |
|
474 |
|
| 590 |
<listitem> |
475 |
<listitem> |
| 591 |
<para>Turn off <emphasis>all</emphasis> |
476 |
<para>¥þ±Ãö³¬ <command>cc</command> ©Ò¯S¦³ªº«D <acronym>ANSI</acronym> C ¼Ð·Ç¥\¯à¡C</para> |
| 592 |
<command>cc</command>'s non-<acronym>ANSI</acronym> C |
|
|
| 593 |
features.</para> |
| 594 |
</listitem> |
477 |
</listitem> |
| 595 |
</varlistentry> |
478 |
</varlistentry> |
| 596 |
</variablelist> |
479 |
</variablelist> |
| 597 |
|
480 |
|
| 598 |
<para>Without these flags, <command>cc</command> will allow you to |
481 |
<para>°£¤F³o¨Ç°Ñ¼Æ¡A<command>cc</command> ÁÙ¤¹³\§A¨Ï¥Î¤@¨ÇÃB¥~ªº°Ñ¼Æ¨ú¥N¼Ð·Ç°Ñ¼Æ¡A¦³¨ÇÃB¥~°Ñ¼Æ«D±`¦³¥Î¡A |
| 599 |
use some of its non-standard extensions to the standard. Some |
482 |
¦ý¬O¹ê»Ú¤W¨Ã¤£¬O©Ò¦³ªº½s;¹³£¦³´£¨Ñ³o¨Ç°Ñ¼Æ¡C |
| 600 |
of these are very useful, but will not work with other |
483 |
·Ó¼Ð·Ç¨Ó¼gµ{¦¡ªº³Ì¥Dn¥Øªº´N¬O¡A§Æ±æ§A¼g¥X¨Óªºµ{¦¡¥i¥H¦b©Ò¦³½s;¹¤W½sĶ¡B°õ¦æµL»~¡A |
| 601 |
compilers—in fact, one of the main aims of the standard is |
484 |
·íµ{¦¡¥i¥H¹F¦¨¤Wz¥Øªº®É¡A´NºÙ¬° <firstterm>portable code(²¾´Ó©Ê¨}¦nªºµ{¦¡½X)</firstterm>¡C</para> |
| 602 |
to allow people to write code that will work with any compiler |
485 |
|
| 603 |
on any system. This is known as <firstterm>portable |
486 |
<para>¤@¯ë¨Ó»¡¡A¦b¼¶¼gµ{¦¡®É´NÀ³nª`·N¡y²¾´Ó©Ê¡z¡C |
| 604 |
code</firstterm>.</para> |
487 |
§_«h¡C·í·Q§âµ{¦¡®³¨ì¥t¥~¤@¥x¾÷¾¹¤W¶]ªº®ÉÔ¡A´N¥i¯à±o»Ýn«¼gµ{¦¡¡C</para> |
| 605 |
|
|
|
| 606 |
<para>Generally, you should try to make your code as portable as |
| 607 |
possible, as otherwise you may have to completely rewrite the |
| 608 |
program later to get it to work somewhere else—and who |
| 609 |
knows what you may be using in a few years time?</para> |
| 610 |
|
488 |
|
| 611 |
<informalexample> |
489 |
<informalexample> |
| 612 |
<screen>&prompt.user; <userinput>cc -Wall -ansi -pedantic -o foobar foobar.c</userinput></screen> |
490 |
<screen>&prompt.user; <userinput>cc -Wall -ansi -pedantic -o foobar foobar.c</userinput></screen> |
| 613 |
</informalexample> |
491 |
</informalexample> |
| 614 |
|
492 |
|
| 615 |
<para>This will produce an executable <filename>foobar</filename> |
493 |
<para>¤Wz«ü¥O·|½T»{ <filename>foobar.c</filename> ¤ºªº»yªk¬O§_²Å¦X¼Ð·Ç¡A |
| 616 |
after checking <filename>foobar.c</filename> for standard |
494 |
¨Ã¥B²£¥Í¦W¬° <filename>foobar</filename> ªº°õ¦æÀÉ¡C</para> |
| 617 |
compliance.</para> |
|
|
| 618 |
|
495 |
|
| 619 |
<variablelist> |
496 |
<variablelist> |
| 620 |
<varlistentry> |
497 |
<varlistentry> |
| 621 |
<term><option>-l<replaceable>library</replaceable></option></term> |
498 |
<term><option>-l<replaceable>library</replaceable></option></term> |
| 622 |
|
499 |
|
| 623 |
<listitem> |
500 |
<listitem> |
| 624 |
<para>Specify a function library to be used at link time.</para> |
501 |
<para>§i¶D gcc ¦b³sµ²(linking)µ{¦¡®É§A»Ýn¥Î¨ìªº¨ç¦¡®w¦WºÙ¡C</para> |
| 625 |
|
502 |
|
| 626 |
<para>The most common example of this is when compiling a |
503 |
<para>³Ì±`¨£ªº±¡ªp´N¬O¡A·í§A¦bµ{¦¡¤¤¨Ï¥Î¤F C ¼Æ¾Ç¨ç¦¡®w¡A |
| 627 |
program that uses some of the mathematical functions in C. |
504 |
¸ò¨ä¥L§@·~¥¥x¤£¤@¼Ëªº¬O¡A³o¨Ç¼Æ¾Ç¨ç¦¡³£¤£¦b¼Ð·Ç¨ç¦¡®w(library)¤¤¡A |
| 628 |
Unlike most other platforms, these are in a separate |
505 |
¦]¦¹½s;¹¨Ã¤£ª¾¹D³o¨ç¦¡®w¦WºÙ¡A§A¥²¶·§i¶D½s;¹n¥[¤W¥¦¤~¦æ¡C</para> |
| 629 |
library from the standard C one and you have to tell the |
506 |
|
| 630 |
compiler to add it.</para> |
507 |
<para>³o³W«h«Ü²³æ¡A¦pªG¦³Ó¨ç¦¡®w¥s°µ <filename>lib<replaceable>something</replaceable>/a</filename>¡A |
| 631 |
|
508 |
´N¥²¶·¦b½sͮɥ[¤W°Ñ¼Æ <option>-l<replaceable>something</replaceable></option> ¤~¦æ¡C |
| 632 |
<para>The rule is that if the library is called |
509 |
Á|¨Ò¨Ó»¡¡A¼Æ¾Ç¨ç¦¡®w¥s°µ <filename>libm.a</filename>¡A |
| 633 |
<filename>lib<replaceable>something</replaceable>.a</filename>, |
510 |
©Ò¥H§A¥²¶·µ¹ <command>cc</command> ªº°Ñ¼Æ´N¬O <option>-lm</option>¡C |
| 634 |
you give <command>cc</command> the argument |
511 |
¤@¯ë±¡ªp¤U¡A³q±`·|§â³o°Ñ¼Æ¥²¶·©ñ¦b«ü¥Oªº³Ì«á¡C |
| 635 |
<option>-l<replaceable>something</replaceable></option>. |
512 |
</para> |
| 636 |
For example, the math library is |
|
|
| 637 |
<filename>libm.a</filename>, so you give |
| 638 |
<command>cc</command> the argument <option>-lm</option>. |
| 639 |
A common <quote>gotcha</quote> with the math library is |
| 640 |
that it has to be the last library on the command |
| 641 |
line.</para> |
| 642 |
|
513 |
|
| 643 |
<informalexample> |
514 |
<informalexample> |
| 644 |
<screen>&prompt.user; <userinput>cc -o foobar foobar.c -lm</userinput> |
515 |
<screen>&prompt.user; <userinput>cc -o foobar foobar.c -lm</userinput> |
| 645 |
</screen> |
516 |
</screen> |
| 646 |
</informalexample> |
517 |
</informalexample> |
| 647 |
|
518 |
|
| 648 |
<para>This will link the math library functions into |
519 |
<para>¤W±³o«ü¥O·|Åý gcc ¸ò¼Æ¾Ç¨ç¦¡®w§@³sµ²¡A¥H«K§Aªºµ{¦¡¥i¥H©I¥s¨ç¦¡®w¤º§tªº¼Æ¾Ç¨ç¦¡¡C</para> |
| 649 |
<filename>foobar</filename>.</para> |
|
|
| 650 |
|
520 |
|
| 651 |
<para>If you are compiling C++ code, you need to add |
521 |
<para>¦pªG§A¥¿¦b½sĶªºµ{¦¡¬O C++ µ{¦¡½X¡A§AÁÙ¥²¶·ÃB¥~«ü©w <option>-lg++</option> ©ÎªÌ¬O |
| 652 |
<option>-lg++</option>, or <option>-lstdc++</option> if |
522 |
<option>-lstdc++</option>¡C |
| 653 |
you are using FreeBSD 2.2 or later, to the command line |
523 |
¦pªG§Aªº FreeBSD ¬O 2.2(§t)¥H«áª©¥»¡A |
| 654 |
argument to link the C++ library functions. |
524 |
§A¥i¥H¥Î«ü¥O <command>c++</command> ¨Ó¨ú¥N <command>cc</command>¡C |
| 655 |
Alternatively, you can run <command>c++</command> instead |
525 |
¦b FreeBSD ¤W <command>c++</command> ¤]¥i¥H¥Î <command>g++</command> ¨ú¥N¡C |
| 656 |
of <command>cc</command>, which does this for you. |
526 |
</para> |
| 657 |
<command>c++</command> can also be invoked as |
|
|
| 658 |
<command>g++</command> on FreeBSD.</para> |
| 659 |
|
527 |
|
| 660 |
<informalexample> |
528 |
<informalexample> |
| 661 |
<screen>&prompt.user; <userinput>cc -o foobar foobar.cc -lg++</userinput> <lineannotation>For FreeBSD 2.1.6 and earlier</lineannotation> |
529 |
<screen>&prompt.user; <userinput>cc -o foobar foobar.cc -lg++</userinput> <lineannotation>For FreeBSD 2.1.6 and earlier</lineannotation> |