Exercise 3-1 (Creating functions and libraries)
Chapter_3 Return | Ifthen Exercise_3-2 |
Exercise 3-1 TCP1, p. 226
Exercise 3-1. Create a header file (with an extension of '.h' [or '.hpp']). In this file, declare a group of functions by varying the argument lists and return values from among the following: void, char, int, and float. Now create a .cpp file that includes your header file and creates definitions for all of these functions. Each definition should simply print out the function name, argument list, and return type so you know it's been called. Create a second .cpp file that includes your header file and defines int main(), containing calls to all of your functions. Compile and run your program.
CONTENTS: functions.hpp functions.cpp main.cpp
functions.hpp download
void f(void);
char g(char, int);
int h(int, float);
int overloaded(float);
void overloaded(int, int);
char overloaded(char, int, int, float);
functions.cpp download
#include "functions.hpp"
#include <iostream>
using std::cout;
using std::endl;
void f(void)
{
cout << "void f (void)" << endl;
}
char g(char c, int i)
{
cout << "char g (char " << c << ", int " << i << ")" << endl;
return 0;
}
int h(int i, float f)
{
cout << "int h (int " << i << ", float " << f << ")" << endl;
return 0;
}
int overloaded(float f)
{
cout << "int overloaded (float " << f << ")" << endl;
return 0;
}
void overloaded(int i, int j)
{
cout << "void overloaded (int " << i << ", int " << j << ")" << endl;
}
char overloaded(char c, int i, int j, float f)
{
cout << "char overloaded (char " << c << ", int " << i
<< ", int " << j << ", float " << f << ")" << endl;
return 0;
}
/*
g++ -c functions.cpp // compile without linking, create `functions.o'
*/
main.cpp download
#include "functions.hpp"
int main()
{
f();
g('a', 0);
h(0, 1.2);
overloaded(2.1);
overloaded(1, 2);
overloaded('a', 0, 1, 2.1);
return 0;
}
/*
g++ -c functions.cpp // compile without linking, create obj file `functions.o'
g++ -c main.cpp // compile without linking, create object file `main.o'
g++ *.o -o main // link object files and create the executable
./main // run program
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)
ar cr libfunctions.a functions.o // create static library
g++ main.o -L. -lfunctions -o main // by default, use shared library (.so)
// if available, otherwise use existing library (.a)
g++ -static main.o -L. -lfunctions -o main // use static library (.a)
./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)
g++ -c -fPIC functions.cpp // create functions.o to be used for shared library
// -fPIC - Position-independent code flag
g++ -shared -fPIC functions.o -o libfunctions.so // create shared library
g++ main.o -L. -lfunctions -o main // make executable using shared library
// by default (now available), otherwise use existing library (.a)
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. // add current directory (.)
// to dynamic linker search path
echo $LD_LIBRARY_PATH
:.
g++ main.o -L. -lfunctions -Wl,-rpath,. -o main // no $LD_LIBRARY_PATH
// -Wl,-rpath,. - pass option -rpath,. to the linker
// -rpath,. - add current directory (.) to run-time search path
./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)
ldd main // print shared libraries used by the program
rm *.o // clean (delete object files)
*/
Note: See Position-independent_code and Rpath on Wikipedia. See also ch2-test and ch2-RTLD on the blog Advanced_Linux_Programming, Chapter_2, Sec. 2.3.
Chapter_3 Return | BACK_TO_TOP | Ifthen Exercise_3-2 |
Comments
Post a Comment