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.
How do you tell the system where to look for a dynamic library?
export LD_LIBRARY_PATH=/path/to/library-dir
This can correct the No such file or directory error when loading a shared library.
What directories do we want our makefiles to use?
build/ to contain binary objects
lib/ to contain all of our libraries
bin/ to contain the executables
Call these
BUILD_DIR = build/$(shell uname -s)-$(uname -m)
LIB_DIR = lib/$(shell uname -s)-$(uname -m)
BIN_DIR = bin/$(shell uname -s)-$(uname -m)
Note that you need to use uname to identify the system.
How do you use uname?
uname -s for the operating sytem
uname -m for the machine architecture
How do you make a directory in a makefile?
I.e
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
What does | do in pre-requestites (dependencies)?
On the left needs to be up to date (timestamps), but both sides need to exist.
Useful for directories.
How do you symbolically link things to the root of the project (where the Makefile is)
my_prog: $(BIN_DIR)/my_prog
ln -sf $(BIN_DIR)/my_prog my_prog
-sf is for symbolic force (force updates/re-creates symlink if needed).
Where does $(LDFLAGS) go?
After $(CFLAGS)
How to use -l (lowercase L)
-l nameoflib
Says to use libnameoflib.so