<<

. 61
( 69 .)



>>

related languages, such as C++, depends largely on
After typing your source code, you can compile it using gcc,
your ability as a programmer, although the Mac OS X
or you can use the make command, which manages the
environment offers excellent support for your development
compilation process. The make command only compiles a
efforts. The gcc compiler that accompanies the Mac OS X
program if the associated binary code is older than the
developer tools is the default compiler for C, C++,
source code. In other words, it only recompiles your
Objective-C, and Objective-C++.
program if you have changed the source code. If your
application consists of multiple source files, you must create
Before you begin working in any of the programming
a Makefile to describe the relationship between those files.
languages available on Mac OS X, you should update your
make calls gcc to compile C programs.
MANPATH to include /usr/share/man. This ensures that the
man pages for the developer tools are available to you. If
make works with many different programming languages,
you type the line setenv MANPATH /sw/share/man:/usr/
although it is most often used with C. In fact, make is not
local/man:/usr/share/man into your .tcshrc file, these and
limited to compiling software. You can read the make man
other pages appear when you use the man command.
page to learn about this versatile command.
You should use the text editor to create your source code.
Many developers prefer emacs because they can configure

DEVELOP C AND C++ APPLICATIONS




„ Type vi hello.c and press ¤ Press I and type #include ‹ Type main () { and press
Return. <stdio.h>, then press Return Return, then press the Tab
twice. key and type printf("Hello,
World\n"); and press Return
– The include command again.
adds the standard IO header
› Type } and press Esc, then
file to your program.
type :wq.
– vi saves your source code.
300
18
DEVELOP UNIX APPLICATIONS


Whenever you develop an application using multiple source files, you can build a
Makefile to define the dependency relationships between files. If you have ever
installed any applications from source code, you have probably used a Makefile
to orchestrate the compilation process.
With a simple program like hello.c, a Makefile is unnecessary. However, when
you are compiling tools such as fetchmail, the Makefile ensures that you compile
all of the proper files and that you create the final executable fetchmail. You
can also write Makefiles to include installation and cleanup instructions. This
allows the user to use the make install and make clean commands to install
an application and remove object files. When you use a Makefile, you do not
need to type commands such as make hello.c. The make command uses the
Makefile in your current directory. With the following Makefile, you only need to
type make to compile hello.c. Be careful to use tabs after the : and before gcc.

Example:
hello: hello.o
gcc hello.o -o hello

Makefiles allow you to control the compilation and installation processes. If
someone else is installing your application, you can give the installer the source
files and a Makefile, to ensure their installation is successful.




– Make calls gcc to compile – Your program runs.
ˇ Type make hello and press Á Type ./hello and press
Return. your program. Return.




301
UNIX FOR MAC

DEVELOP JAVA APPLICATIONS
You normally precede code like this with a line or two of

Y
ou can develop Java applications on your Mac OS X
comments. Java comments start with /* and end with */
system. You run Java applications with the java
and can span multiple lines. Single-line comments can
interpreter, but you first compile them with a
start with // and need nothing else. Comments that
program called javac. Examination of the HelloWorld
start with /** are special comments, and a tool called
program below illustrates why you need a compiler to
javadoc ” which is intended to provide automated code
compile Java code into a special intermediate form called
documentation ” extracts the text from these comments.
bytecode, and an interpreter to run the compiled code.
The javadoc command produces documentation for
public classes.
The following lines of code represent the HelloWorld
program written in Java. The single print command is
To compile the HelloWorld.java program, use the javac
similar to the printf command in C. The second line
command. The command javac HelloWorld.java reads
defines the main method and is the entry point for every
your source code and produces a class file, HelloWorld.
Java application. In Java, each method or function exists
class. You can then run this program with the command
within a class or an object, where an object is an instance of
java HelloWorld.
a class. Thus, you must enclose this simple program for
printing a single line of text within a class definition.
Java applications are often distributed as Java archive (JAR)
files, and you can list the contents with the jar command.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
DEVELOP JAVA APPLICATIONS




– The Pico screen appears. ¤ Type /** and press Return, › Type public static void
CREATE A JAVA PROGRAM
type * This is Hello World main(String[] args) { and press
„ Start the Pico editor
written in Java and press Return.
to create a file named
Return, then type */
ˇ Type System.out.println
HelloWorld.java and
and press Return.
press Return. ("Hello, World!"); and press
– Your javadoc comment is Return.
complete.
Á Type } and press Return,
‹ Type public class then type } again.
302 HelloWorld { and press Return.
18
DEVELOP UNIX APPLICATIONS


You can create a Web page containing the javadoc
You can create a Java archive from your
comments from your HelloWorld application by using
HelloWorld application with the jar
the javadoc command. Javadoc comments provide
command. Syntactically, the jar
easy to access documentation for Java programs and
command is like the tar command. To
are generally considered essential to proper Java
create a JAR file, you can use the jar
programming. These comments are especially critical
cvf command. To extract from a JAR file,
when multiple programmers work on the same project.
you can use the jar xvf command. To
list the contents of a JAR file, you can use
To view extracted javadoc comments, use the open
the jar tvf command.
command, as in open HelloWorld.html. This will
open the javadoc Web page in a browser.
RESULT:
TYPE THIS:
RESULT:
TYPE THIS:
0 Thu May 29
[ferro:˜] user%
21:18:32 EDT 2003
jar cvf
META-INF/
HelloWorld.jar -rw-r”r” 1 user staff 427
[ferro:˜] user%
HelloWorld.class May 29 20:57 HelloWorld.class
javadoc
67 Thu May 29
HelloWorld.java
21:18:32 EDT 2003
[ferro:˜] user% -rw-r”r” 1 user staff 6967
META-INF/MANIFEST.MF
jar tvf May 29 20:58 HelloWorld.html
[ferro:˜] user%
HelloWorld.jar ls -l
427 Thu May 29 -rw-r”r” 1 user staff 754
HelloWorld*
20:57:50 EDT 2003 May 29 21:18 HelloWorld.jar
HelloWorld.class
-rw-r”r” 1 user staff 181
May 29 20:57 HelloWorld.java




– The system lists your
‡ Save your file and exit · Type java HelloWorld and ‚ Type ls -l HelloWorld* and
Pico. press Return. press Return. source file and Java program.
– Your program runs.
° Type javac HelloWorld.java
and press Return.
– Your program compiles.


303
UNIX FOR MAC



DEVELOP PYTHON APPLICATIONS
be transitional languages, because they allow you to write

Y
ou can develop Python applications on your Mac OS X
programs while you are only beginning to grasp the
system. Python installs with the Mac OS X Developer
concepts of object-oriented programming.
Tools as /usr/bin/python. The command which
python prints this path; if it does not, you must check your
Languages like C and scripting languages like Perl are
PATH variable and this location.
procedural languages. This means that portions of code are
organized into functional elements called procedures or
Python is an interpreted, interactive, object-oriented
subroutines that you can use in the remainder of the code.
programming language. Unlike C programs which you
Object-oriented languages work differently in that they define
compile, and Java programs which you run on top of a
objects and then a set of operations that you can perform on
virtual machine, Python programs are more like Perl scripts.
these objects. You refer to these operations as methods.
However, the object orientation of Python gives Python
programs a distinctively different character.
Where Perl prints to a file using a command such as print
LOG "$num records processed";, Python uses a
Even if you are not already familiar with object-oriented
command such as out_file.write("Testing,
programming, you can still write useful Python scripts.
testing, 1, 2, three\nThat's all folks\n").
However, to make full use of the language, you must
While, on the surface, these commands may not seem very
develop an understanding of object-oriented programming.
different, the implementation and coding method are
In fact, many people consider languages such as Python to
radically different, and you encounter these differences as
you write your own methods.
DEVELOP PYTHON APPLICATIONS




‹ Type print "Hello, World!" ˇ Type python HelloWorld.py ‡ Type ./HelloWorld.py and
CREATE HELLO WORLD
PROGRAM IN PYTHON and press Return. and press Return. press Return.
„ Start Pico to create a file – Your script runs. – Your script runs again.
› Save your script and exit
name HelloWorld.py. Pico.
Á Type chmod a+x
¤ Type #!/usr/bin/python and – Pico saves your script. HelloWorld.py and press
press Return. Return.

304
18
DEVELOP UNIX APPLICATIONS

While a program as simple as HelloWorld does not display the object orientation of
Python, creating and working with objects reveals some of the advantages of using

<<

. 61
( 69 .)



>>