Exercise 3-30 (YourPets, makefile in C/C++, suffix rules)

Chapter_3     Exercise_3-29     Exercise_3-34 Exercise_3-35     Exercise_3-31







Exercise 3-30     TCP1, p. 230


Exercise 3-30. Create a makefile that not only compiles YourPets1.cpp and YourPets2.cpp (for your particular compiler, see ch3-YourPets 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.




Folder ex3-30 contains the following files:
ReadMe.txt     Makefile     yourpets.c     YourPets.cpp




Note:  See also ch1-make on the blog Advanced_Linux_Programming, Chapter_1, Sec. 1.3.




ReadMe.txt         download


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
...











Makefile         download


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











yourpets.c         download


#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)
*/











YourPets.cpp         download


#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)
*/









Chapter_3     Exercise_3-29     Exercise_3-34 BACK_TO_TOP Exercise_3-35     Exercise_3-31



Comments

Popular posts from this blog

Contents