Warning! This site is under development.
This class may be recorded! Its use will be limited to members of the class. Please do not share with others.
Approximate overview
make?SE context:
Historical context:
What happens between the C code and the executable?
(We’ll consider both traditional compilation and more modern compilation.)
We want to go from foo.c to the foo executable, paying attention to all the steps.
#define‘d names with the corresponding text.#include‘d#ifdef sections.What are libraries?
-lm to include the math library.What goes in a (non-trival) C project?
Libraries are collections of code we expect to others to use in their programs, or that we expect ourselves to use in other contexts. Good programmers find or write libraries of things they want to use.
Libraries in C are often loaded with -lNAME.
Have you built a library in C? No.
Examples: Sam wants to build a library of a variety of math functions that Sam uses regularly.
gcd (long x, long y)expmod (long x, long n, long m) compute x^n mod mWe will be building srmath.so, at least I think we will.
What files will we need or build.
srgcd.c contains the code for the gcd function.srgcd.o with the object code for gcd. Build with something like
cc -c srgcd.c -o srgcd.osrmath.h declares all of the functions in the library.gcdtest.cgcdtest.ogcd, an excutable, because every function should also be a command you can type.gcd.c, the source code for the executable (not including the gcd function)
includes the main routines.gcd.osrmath.a or srmath.so The library file that we will be creatingREADME to provide guidance to those who come after us.LICENSE to indicate how the materials can be used.Makefile to remember all of the instructions.To ease shared development and to speed rebuilding, we regularly divide larger projects into many files.
Remember: Instructions for building the parts of a program and also for determining what needs to be rebuilt when a file changes.
TARGET: PREREQUSITES
INSTRUCTIONS
INSTRUCTIONS
INSTRUCTIONS
We’re building a sample math utilities package, which we can find in
examples/srmath.
Right now, srmath presents one function, gcd (long, long).
What we start with
$ ls
gcd.c gcdtest.c srgcd.c srmath.h srtest.c srtest.h testdemo.c
It appears that Sam made a bad design decision and temporarily put
gcd(long long) into both srgcd.c and srmath.c. WE just got
rid of the latter.
gcd is declared in srmath.hLet’s write instructions for building the gcd application.
How do we combine gcd.c and srgcd.c into the executable gcd?
cc -c gcd.c -o gcd.occ -c srgcd.c -o srgcd.occ gcd.o srgcd.o -o gcdAnd what dependencies exist?
gcd: gcd.o srgcd.o
cc gcd.o srgcd.o -o gcd
gcd.o: gcd.c srmath.h
cc -c gcd.c -o gcd.o
srgcd.o: srgcd.c srmath.h
cc -c srgcd.c -o srgcd.o
$ make gcd
cc -c gcd.c -o gcd.o
cc -c srgcd.c -o srgcd.o
cc gcd.o srgcd.o -o gcd
Most makefiles have a standard set of targets.
default (which should come first): The primary thing we want to
buildclean: Remove temporary files, keep built things that are likely
useful (e.g., executables or the library).distclean: Remove temporary files and any other built things.
(Clean up for distribution to others.)$@ the target $^ the dependencies $< the first dependency