Lab Exam : Makefile Review Flashcards
How do you make gcc not link what it makes?
Use the -c option.
When -c is omitted, it looks for a main function for the entry point.
What recipe compiles an object file?
module1.o: module1.c
$(CC) -c $(CFLAGS) module1.c -o module1.o
What recipe links the files?
my_prog: module1.o module2.o
$(CC) $(CFLAGS) module1.o module2.o my_prog
CFLAGS
-Wall -Wextra -pedantic -std=c99 -g
CC
gcc
What do header files contain?
.h files contain function prototypes (or “headers”), type definitions, and useful macros.
What files need to include header files?
Files that implement the functions listed or use the functions listed.
-I (captial i) option
Adds a place to search to the preprocessor’s list. This gives the preprocessor an additional place to search.
I.e. -I. for the current directory
Note that there can be many -I flags
What do you store -I (capital I) in?
CPPFLAGS
How do you write a header file?
ifndef FILENAME_H
#define FILENAME_H
//stuff goes here
#endif
How do you link files together into a static library?
ar rcs libname.a module1.o module2.o ….
This creates a library called libname.a which contains the object files.
Note that they MUST begin with “lib” and end with “.a”.
(or “.so” for dynamic libraries).
How do you link a library during the linking step?
use -L and -l (lowercase L) options when invoking gcc.
-L followed by the directory (i.e. -L/home/…) says where to look for library files.
-l followed by the name of a library tells gcc to link a specific library from one of the locations given by -L.
I.e.
-lname-name tells gcc to find libname-name.a
Use LDFLAGS for these.
Where do you put the -L flags?
In LDFLAGS
How to make a shared libary?
$(CC) -shared $(CFLAGS) module1.o module2.o …. -o libname.so
Do you need to tell gcc the -L and -l options for dynamic libraries?
Yes. It still needs to be able to “search” for a particular dynamic library.