Lecture 15: Libraries
Monday, March 26, 2012[Up] [Previous Lecture] [Next Lecture]
Reading Assigned: 3.2 (especially page 121)
Homework Due: None
Homework Assigned: Homework 07 -- assigned last class
Classwork Assigned: None
Topics Covered:
- Homework 7 and Homework X questions
- Grades in blackboard
- Libraries
- Libraries allow you to reuse existing code without writing it yourself
- Allows you (or the library creator) to package up common functions into logical units
- Limits program size by only pulling in code actually used
- The linker is the program that actually combines libraries and your source code
- gcc is a frontend that calls into the compiler as well as the linker (ld)
- Libraries we've used so far:
- stdio.h (printf and scanf)
- Libraries we'll go over today
- stdlib.h (abs, rand, exit, abort, etc.)
- math.h (cos, sin, tan, pow, log10, fabs, etc.)
- C standard library (stdlib.h)
- Online reference
- Provides functions for memory management (malloc, free, calloc, realloc)
- Provides functions for interacting with Linux (system, exit, abort, atexit)
- Provides simple search functions (bsearch, qsort)
- Provides functions for converting strings to numbers (atof, atol, etc.)
- Provides a few simple integer math functions (abs, div)
- Once you #include <stdlib.h> you can use all these functions
- C math library (math.h)
- Provides access to common mathematical functions (trigonometric, exponential, etc.)
- Online reference
- Almost all functions operate on double arguments
- Used by specifying: #include <math.h>
- When compiling must add an additional option "-lm" after your .c file and before "-o", eg.
linux[1]% gcc -Wall myprog.c -o myprog /tmp/ccOEaskr.o: In function `main': myprog.c:(.text+0x2d): undefined reference to `pow' collect2: ld returned 1 exit status linux[2]% gcc -Wall myprog.c -lm -o myprog linux[3]% ./myprog
- The -l option indicates that the linker should include an additional library (libm.a)