Personal Libraries Lecture 17 Engineering H192 - Computer Programming

advertisement
Engineering H192 - Computer Programming
Personal Libraries
Lecture 17
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 1
Engineering H192 - Computer Programming
The Steps in Making a Program
1) Create the program source file.
2) Compile to produce an object file.
3) Link with other object files (stored in a library)
to produce and executable file.
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 2
Engineering H192 - Computer Programming
Creating Personal Libraries
To create a personal library, we first create a special
kind of source code file called a header file. It is a
text file that contains all the information the
compiler needs about the things in the library.
This information might include:
Comments
#define directives
Function prototypes for the functions that
are in the personal library.
This file would have a " .h " file extension
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 3
Engineering H192 - Computer Programming
Creating the "Implementation" File
• We must also create another special kind of C
source code file called an implementation file.
• This file might include:
– Comments
– #include directives
– #define directives (for constant macros
used only inside this library)
– Type definitions used only in this library.
– Function definitions for the functions you
want in the library.
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 4
Engineering H192 - Computer Programming
Creating an Object File
• The implementation file is saved with a " .c " or
" .cpp " extension and is must be compiled before
it is used:
>CC -c mylib.cpp
• The " -c " switch means "compile only"
• This produces an object file called mylib.o
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 5
Engineering H192 - Computer Programming
Creating Personal Libraries
• Note that the mylib.o object file is NOT a "true"
library file. It is analogous to a book that we want
to put into a library.
• We can create a true library file and put a copy of
our object file into the library with the UNIX
archive command:
>ar -r mylib.lib mylib.o
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 6
Engineering H192 - Computer Programming
Creating Personal Libraries
• Since our file contains three functions, it usually
would be better to separate them into three files
such as square.cpp, cube.cpp, and factorial.cpp.
• Then we would compile each one of these
separately to produce square.o, cube.o and
factorial.o object files.
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 7
Engineering H192 - Computer Programming
Maintaining Personal Libraries
• Then we could remove mylib.o from our library:
>ar -d mylib.lib mylib.o
and place into it our three new object files:
>ar -r mylib.lib square.o cube.o factorial.o
• It we want to see the contents of our library file
we can get a table of contents with:
>ar -t mylib.lib
or
>nm mylib.lib
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 8
Engineering H192 - Computer Programming
Using a Personal Library
• Put an include statement in your program file
#include "mylib.h"
Note the quotes (" ") not angle brackets (<>)
• Put the names of both your program file
and your library file in the compile/link
command.
• Compile and link the program with the library:
>CC -o e16.out e16.cpp mylib.lib
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 9
Engineering H192 - Computer Programming
Assignment E16
• Three part assignment:
– Create your own header file, mylib.h.
– Make your own library file in two steps:
• 1). Create library source file, mylib.cpp
• 2). Compile mylib.cpp to produce mylib.o
(You are not required to make a "true" UNIX
library file for this assignment.)
– Create your test program, e16.cpp.
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 10
Engineering H192 - Computer Programming
A Recursive Factorial Function
// From Deitel & Deitel Chap 5
long factorial (long number)
{
if ( number <= 1 )
return 1 ;
else
return ( number * factorial (number - 1)) ;
}
Winter Quarter
Gateway Engineering Education Coalition
Lect 17
P. 11
Download