Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 1Winter Quarter Personal Libraries.

Similar presentations


Presentation on theme: "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 1Winter Quarter Personal Libraries."— Presentation transcript:

1 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 1Winter Quarter Personal Libraries Lecture 17

2 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 2Winter Quarter 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 an executable file.

3 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 3Winter Quarter 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

4 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 4Winter Quarter 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. <<

5 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 5Winter Quarter Creating an Object File The implementation file is saved with a ".cpp " extension and is must be compiled before it is used: >g++ mylib.cpp -c The " -c " switch means "compile only" This produces an object file called mylib.o

6 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 6Winter Quarter 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 (ar) command: >ar -r mylib.lib mylib.o

7 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 7Winter Quarter Creating Personal Libraries Since our file contains two or three functions, it usually would be better to separate them into three files such as square.cpp, cube.cpp, and (optionally) factorial.cpp. Then we would compile each one of these separately to produce square.o, cube.o and factorial.o object files.

8 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 8Winter Quarter 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

9 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 9Winter Quarter 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: >g++ -o g15.out g15.cpp mylib.lib

10 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 10Winter Quarter Assignment G15 Three steps in the 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, g15.cpp.

11 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 11Winter Quarter A Recursive Factorial Function (Optional) // From Deitel & Deitel Chap 5 Section 13 long factorial (long number) { if ( number <= 1 ) return 1 ; else return ( number * factorial (number - 1)) ; }


Download ppt "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 17P. 1Winter Quarter Personal Libraries."

Similar presentations


Ads by Google