Docsity

Personal Libraries - Intro to Computer Programming - Lecture Slides, Slides of Computer Engineering and Programming

Partial preview of the text

Download Personal Libraries - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

Personal Libraries

Lecture 17

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.

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. <<

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

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.

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

Assignment F

  • 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.

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)) ;

} Docsity.com