Open the terminal in folder reciprocal (Ctrl+Alt+T or F4).
Compile without linking:
gcc -c -I ./include ./src/main.c
gcc -c -D NDEBUG=3 -O2 -I ./include ./src/main.c
g++ -c -I ./include ./src/reciprocal.cpp
g++ -c -D NDEBUG=3 -O2 -I ./include ./src/reciprocal.cpp
Link object files and output the executable `reciprocal':
g++ main.o reciprocal.o -o reciprocal
g++ *.o -o reciprocal
Run the program:
./reciprocal
Usage: ./reciprocal n
n - integer
./reciprocal 1
The reciprocal of 1 is 1
./reciprocal 10
The reciprocal of 10 is 0.1
./reciprocal 7
The reciprocal of 7 is 0.142857
./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:
rm *.o
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#ifdef __cplusplus
extern "C"
{
#endif
extern double reciprocal (int i);
#ifdef __cplusplus
}
#endif
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <cassert> // for assert()
#include "reciprocal.hpp" // for reciprocal()
double reciprocal (int i)
{
// i should be non-zero
assert (i != 0);
return 1.0 / i;
}
/*
g++ -c -I ./include ./src/reciprocal.cpp
g++ -c -D NDEBUG=3 -O2 -I ./include ./src/reciprocal.cpp
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), fprintf(), stderr
#include <stdlib.h> // for atoi()
#include "reciprocal.hpp" // for reciprocal()
int main (int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./reciprocal n\n");
fprintf(stderr, "n - integer\n");
return 1; // end program, signalling error
}
int i;
i = atoi (argv[1]);
printf ("The reciprocal of %d is %g\n", i, reciprocal (i));
return 0; // end program normally
}
/*
gcc -c -I ./include ./src/main.c // compile without linking
gcc -c -D NDEBUG=3 -O2 -I ./include ./src/main.c
g++ -c -I ./include ./src/reciprocal.cpp
g++ -c -D NDEBUG=3 -O2 -I ./include ./src/reciprocal.cpp
Link object files and output the executable `reciprocal':
g++ main.o reciprocal.o -o reciprocal
g++ *.o -o reciprocal
./reciprocal
Usage: ./reciprocal n
n - integer
./reciprocal 1
The reciprocal of 1 is 1
./reciprocal 10
The reciprocal of 10 is 0.1
./reciprocal 7
The reciprocal of 7 is 0.142857
./reciprocal 0
The reciprocal of 0 is inf
rm *.o // clean
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
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
clean:
rm *.o reciprocal
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
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
make CFLAGS=-g
gcc -g -I ./include -c ./src/main.c
g++ -g -I ./include -c ./src/reciprocal.cpp
g++ -g main.o reciprocal.o -o reciprocal
gdb reciprocal
(gdb) run 0
// reciprocal: ./src/reciprocal.cpp:7: double reciprocal(int):
// Assertion `i != 0' failed.
// Program received signal SIGABRT, Aborted.
(gdb) where
#0 ...
#1 ...
#2 assertion=... "i != 0",
file=... "./src/reciprocal.cpp", line=7,
#3 function=... "double reciprocal(int)")
#4 in reciprocal (i=0) at ./src/reciprocal.cpp:7
#5 in main (argc=2, argv=...) at ./src/main.c:17
(gdb) up
#1 ...
(gdb) up 3
#4 ...
(gdb) print i
// $1 = 0
(gdb) down
#3 ...
(gdb) down 2
#1 ...
(gdb) next
// Program terminated with signal SIGABRT, Aborted.
// The program no longer exists.
(gdb) break main
// Breakpoint 1, main (argc=..., argv=...) at ./src/main.c:6
(gdb) break reciprocal
// Breakpoint 2 at ...: file ./src/reciprocal.cpp, line 5.
(gdb) run 0
// Breakpoint 1, main
(gdb) next
// if (argc != 2)
(gdb) next
// i = atoi (argv[1]);
(gdb) next
// printf ("The reciprocal of %d is %g\n", i, reciprocal (i));
(gdb) step
// Breakpoint 2, reciprocal
(gdb) step
// assert (i != 0);
(gdb) step
// __GI___assert_fail (assertion=... "i != 0",
// file=... "./src/reciprocal.cpp", line=7,
// function=... "double reciprocal(int)")
(gdb) next
// reciprocal: ./src/reciprocal.cpp:7: double reciprocal(int):
// Assertion `i != 0' failed.
// Program received signal SIGABRT, Aborted.
(gdb) next
// Program terminated with signal SIGABRT, Aborted.
// The program no longer exists.
(gdb) next
// The program is not being run.
(gdb) q // quit debugger
make clean
rm *.o