Exercise 3-30. Create a makefile that not only compiles YourPets1.cpp and YourPets2.cpp (for your particular compiler, see YourPets.cpp and Exercise_3-6 in Section ch3-Data_Types) but also executes both programs as part of the default target behavior. Make sure you use suffix rules.
Open the terminal in folder ex3-30.
Compile and run programs:
make // make all
./yourpets
sizeof(int): 4
f(): 93872301691646
g(): 93872301691241
dog: 93872301703188
cat: 93872301703200
bird: 93872301703192
fish: 93872301703196
main(): 93872301691252
i: 140732589031580
j: 140732589031584
k: 140732589031588
pet id number: 0
pet id number: 0
pet id number: 0
pet id number: 0
./YourPets
sizeof(int): 4
f(): 94709218202929
g(): 94709218202153
dog: 94709218214228
cat: 94709218214232
bird: 94709218214236
fish: 94709218214240
main(): 94709218202164
i: 140729803011580
j: 140729803011584
k: 140729803011588
pet id number: 0
pet id number: 0
pet id number: 0
pet id number: 0
Delete object files:
make clean
rm *.o
Only compile programs:
make compile
gcc -c yourpets.c
gcc yourpets.o -o yourpets
g++ -c YourPets.cpp
g++ YourPets.o -o YourPets
Run programs (if already compiled):
make // make all
./yourpets
...
./YourPets
...
****************************************************************************************
****************************************************************************************
****************************************************************************************
C = gcc
CPP = g++
OFLAG = -o
.SUFFIXES : .o .c .cpp
.c.o :
$(C) $(CFLAGS) -c $<
.cpp.o :
$(CPP) $(CPPFLAGS) -c $<
all: yourpets YourPets # compile and run programs
./yourpets
./YourPets
compile: yourpets YourPets # only compile, don't run programs
yourpets: yourpets.o
$(C) yourpets.o $(OFLAG) yourpets
YourPets: YourPets.o
$(CPP) YourPets.o $(OFLAG) YourPets
yourpets.o: yourpets.c
YourPets.o: YourPets.cpp
clean: # delete object files
rm *.o
****************************************************************************************
****************************************************************************************
****************************************************************************************
#include <stdio.h> // for printf()
int dog, cat, bird, fish; // global vars initialized to 0
int main(); // can be declared before its definition
void f(int pet); // defined after main()
void g(void){} // defined before main()
int main()
{
int i, j, k;
printf("sizeof(int): %lu\n", sizeof(int));
printf("f():\t%ld\n", (long)&f);
printf("g():\t%ld\n", (long)&g);
printf("dog:\t%ld\n", (long)&dog);
printf("cat:\t%ld\n", (long)&cat);
printf("bird:\t%ld\n", (long)&bird);
printf("fish:\t%ld\n", (long)&fish);
printf("main():\t%ld\n", (long)&main);
printf("i:\t%ld\n", (long)&i);
printf("j:\t%ld\n", (long)&j);
printf("k:\t%ld\n", (long)&k);
f(dog), f(cat), f(bird), f(fish), g(); // multiple function calls on one line
return 0;
}
void f(int pet)
{
printf("pet id number: %d\n", pet);
}
/*
gcc yourpets.c -o yourpets
./yourpets
sizeof(int): 4
f(): 93915944956670 // f() defined after main()
g(): 93915944956265 // g() defined before main()
dog: 93915944968212 // dog, bird, fish, cat
cat: 93915944968224
bird: 93915944968216
fish: 93915944968220
main(): 93915944956276 // after g(), before f()
i: 140722843449788
j: 140722843449792
k: 140722843449796
pet id number: 0 // f(dog)
pet id number: 1 // f(cat)
pet id number: 2 // f(bird)
pet id number: 3 // f(fish)
*/
****************************************************************************************
****************************************************************************************
****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int dog, cat, bird, fish; // global vars initialized to 0
int main(); // can be declared before its definition
void f(int pet); // defined after main()
void g(void){} // defined before main()
int main()
{
int i, j, k;
cout << "sizeof(int): " << sizeof(int) << endl;
cout << "f():\t" << (long)&f << endl;
cout << "g():\t" << (long)&g << endl;
cout << "dog:\t" << (long)&dog << endl;
cout << "cat:\t" << (long)&cat << endl;
cout << "bird:\t" << (long)&bird << endl;
cout << "fish:\t" << (long)&fish << endl;
cout << "main():\t" << (long)&main << endl;
cout << "i:\t" << (long)&i << endl;
cout << "j:\t" << (long)&j << endl;
cout << "k:\t" << (long)&k << endl;
f(dog), f(cat), f(bird), f(fish), g(); // multiple function calls on one line
return 0;
}
void f(int pet)
{
cout << "pet id number: " << pet << endl;
}
/*
g++ YourPets.cpp -o YourPets
./YourPets
sizeof(int): 4
f(): 94641376949411 // f() defined after main()
g(): 94641376948745 // g() defined before main()
dog: 94641376960852
cat: 94641376960856
bird: 94641376960860
fish: 94641376960864
main(): 94641376948756 // after g(), before f()
i: 140721441130380
j: 140721441130384
k: 140721441130388
pet id number: 0 // f(dog)
pet id number: 0 // f(cat)
pet id number: 0 // f(bird)
pet id number: 0 // f(fish)
*/
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
Exercise 3-35. Create a makefile for one of the previous exercises (of your choice) that allows you to type make for a production build of the program, and make debug for a build of the program that includes debugging information.
Open the terminal in folder ex3-35.
Compile program:
make // make all
g++ -c functions.cpp
g++ -c main.cpp
g++ functions.o main.o -o main
Run program:
make run
./main
void f (void)
char g (char a, int 0)
int h (int 0, float 1.2)
int overloaded (float 2.1)
void overloaded (int 1, int 2)
char overloaded (char a, int 0, int 1, float 2.1)
Delete object files:
make clean
rm *.o
Compile program with debugging information:
make CPPFLAGS=-g
g++ -g -c functions.cpp
g++ -g -c main.cpp
g++ functions.o main.o -o main
make debug
g++ -g -c functions.cpp
g++ -g -c main.cpp
g++ functions.o main.o -o main
Run program with debugging enabled:
make run
./main
void f (void)
char g (char a, int 0)
int h (int 0, float 1.2)
int overloaded (float 2.1)
void overloaded (int 1, int 2)
char overloaded (char a, int 0, int 1, float 2.1)
****************************************************************************************
****************************************************************************************
****************************************************************************************
CPP = g++
OFLAG = -o
# debug flag:
DBGFLAG = -g
.SUFFIXES : .o .cpp
.cpp.o :
$(CPP) $(CPPFLAGS) -c $<
all: main
debug: functions.cpp main.cpp
$(CPP) $(CPPFLAGS) $(DBGFLAG) -c functions.cpp
$(CPP) $(CPPFLAGS) $(DBGFLAG) -c main.cpp
$(CPP) functions.o main.o $(OFLAG) main
run: main
./main
main: functions.o main.o
$(CPP) functions.o main.o $(OFLAG) main
functions.o: functions.cpp
main.o: main.cpp
clean: # delete object files
rm *.o