Program as a single file
Program created as five s eparate files
Compile as one file and execute
- cat myinclude.h main.c input.c factorial.c output.c > total.c
- gcc -o testfact total.c
- ./testfact
- cis-lclient20:~/2012/c/make>gcc total.c
- cis-lclient20:~/2012/c/make>a.out
input a number - 5
The length of f is 1
The factorial is 120
- cis-lclient20:~/2012/c/make>a.out
input a number - 10
The length of f is 1
The factorial is 0
Compile each file separately to create four object files, then link into an executable
- gcc -c input.c
- gcc -c output.c
- gcc -c factorial.c
- gcc -c main.c
- ls *.o
factorial.o input.o main.o output.o
- gcc -o testfac main.o input.o factorial.o output.o
- ./testfac
input a number - 5
The length of f is 1
The factorial is 120
Use a shell script to compile each file separately
- cis-lclient06:~/2012/c/make>more shellscript
#!/bin/sh
# remember "chmod 755 your-script-name"
gcc -c input.c # produces input.o
gcc -c output.c # produces output.o
gcc -c factorial.c # products factorial.o
gcc -c main.c # produces main.o
gcc -o testfac main.o input.o factorial.o output.o
./testfac
- cis-lclient06:~/2012/c/make>ls *.o
ls: No match.
- cis-lclient06:~/2012/c/make>shellscript
input a number - 5
The length of f is 1
The factorial is 120
- cis-lclient06:~/2012/c/make>ls *.o
factorial.o input.o main.o output.o
makefile to compile and link, long version
- cis-lclient20:~/2012/c/make>more makefile1
testfac: main.o input.o factorial.o output.o
gcc main.o input.o factorial.o output.o -o testfac
# gcc main.o input.o factorial.o output.o -o $@
main.o: main.c myinclude.h
gcc -c main.c
input.o: input.c myinclude.h
gcc -c input.c
factorial.o: factorial.c myinclude.h
$(CC) -c factorial.c
output.o: output.c myinclude.h
$(CC) -c output.c
clean:
-rm -f *.o testfac core
all:
gcc -c main.c input.c factorial.c output.c
gcc main.o input.o factorial.o output.o -o testfac
makefile to compile and link
cis-lclient20:~/2012/c/make>more makefile
OBJ = main.o input.o factorial.o output.o
SOURCE = main.c input.c factorial.c output.c
CC = gcc
testfac: ${OBJ}
${CC} -o $@ ${OBJ}
main.o: myinclude.h
input.o: myinclude.h
factorial.o: myinclude.h
output.o: myinclude.h
clean:
-rm -f *.o testfac core
all:
gcc -c ${SOURCE}
gcc -o testfac ${OBJ}
cis-lclient20:~/2012/c/make>
cis-lclient20:~/2012/c/make>make clean
rm -f *.o testfac core
cis-lclient20:~/2012/c/make>make
gcc -c -o main.o main.c
gcc -c -o input.o input.c
gcc -c -o factorial.o factorial.c
gcc -c -o output.o output.c
gcc -o testfac main.o input.o factorial.o output.o
cis-lclient20:~/2012/c/make>make
make: `testfac' is up to date.
cis-lclient20:~/2012/c/make>testfac
input a number - 5
The length of f is 1
The factorial is 120
Change char to long long
- cis-lclient20:~/2012/c/make>nano myinclude.h
/* my include file */
#include <stdio.h>
#define INTTYPE unsigned long long
INTTYPE factorial(INTTYPE);
INTTYPE input();
void output(INTTYPE);
- cis-lclient20:~/2012/c/make>make
gcc -c -o main.o main.c
gcc -c -o input.o input.c
gcc -c -o factorial.o factorial.c
gcc -c -o output.o output.c
gcc -o testfac main.o input.o factorial.o output.o
- cis-lclient20:~/2012/c/make>testfac
input a number - 20
The length of f is 8
The factorial is 2432902008176640000
- cis-lclient20:~/2012/c/make>