ch1-make

Chapter_1     reciprocal debug







CONTENTS:     Makefile     readme.txt




Note:  We added the file Makefile to folder_reciprocal (see reciprocal).




Makefile     ALP, p. 21


reciprocal: main.o reciprocal.o
g++ $(CFLAGS) main.o reciprocal.o -o reciprocal
main.o: ./src/main.c ./include/reciprocal.hpp
gcc $(CFLAGS) -I ./include -c ./src/main.c
reciprocal.o: ./src/reciprocal.cpp ./include/reciprocal.hpp
g++ $(CFLAGS) -I ./include -c ./src/reciprocal.cpp
run: reciprocal
./reciprocal $(ARGS)
clean:
rm *.o





Note:

The clean target could also remove the executable:
clean:
rm *.o reciprocal











Note:  We added the new tests to readme.txt (compare to readme.txt in reciprocal).




readme.txt     ALP, p. 22


Open the terminal in folder reciprocal (Ctrl+Alt+T or F4).

Compile without linking:
make main.o
gcc -c -I ./include ./src/main.c
make main.o CFLAGS="-D NDEBUG=3 -O2"
gcc -c -D NDEBUG=3 -O2 -I ./include ./src/main.c

make reciprocal.o
g++ -c -I ./include ./src/reciprocal.cpp
make reciprocal.o CFLAGS="-D NDEBUG=3 -O2"
g++ -c -D NDEBUG=3 -O2 -I ./include ./src/reciprocal.cpp

Link object files and output the executable `reciprocal':
make reciprocal
make
g++ main.o reciprocal.o -o reciprocal
g++ *.o -o reciprocal

Run the program:
make run
./reciprocal
Usage: ./reciprocal n
n - integer
make: *** [Makefile:8: run] Error 1

make run ARGS=1
./reciprocal 1
The reciprocal of 1 is 1

make run ARGS=10
./reciprocal 10
The reciprocal of 10 is 0.1

make run ARGS=7
./reciprocal 7
The reciprocal of 7 is 0.142857

make run ARGS=0
./reciprocal 0
The reciprocal of 0 is inf

NDEBUG disables run-time errors, to focus on programming errors. See:
https://cplusplus.com/reference/cassert/assert/

Clean:
make clean
rm *.o

make run ARGS=1
gcc -I ./include -c ./src/main.c
g++ -I ./include -c ./src/reciprocal.cpp
g++ main.o reciprocal.o -o reciprocal
./reciprocal 1
The reciprocal of 1 is 1

make run ARGS=0
./reciprocal 0
reciprocal: ./src/reciprocal.cpp:7: double reciprocal(int): Assertion `i != 0' failed.
make: *** [Makefile:8: run] Aborted (core dumped)

make clean
rm *.o

make run CFLAGS="-D NDEBUG=3 -O2" ARGS=1
S="-D NDEBUG=3 -O2" ARGS=1
gcc -D NDEBUG=3 -O2 -I ./include -c ./src/main.c
g++ -D NDEBUG=3 -O2 -I ./include -c ./src/reciprocal.cpp
g++ -D NDEBUG=3 -O2 main.o reciprocal.o -o reciprocal
./reciprocal 1
The reciprocal of 1 is 1

make run ARGS=0
./reciprocal 0
The reciprocal of 0 is inf

make clean
rm *.o









Chapter_1     reciprocal BACK_TO_TOP debug



Comments

Popular posts from this blog

Contents