pdf for printing - Image Processing and Analysis Group
pdf for printing - Image Processing and Analysis Group
pdf for printing - Image Processing and Analysis Group
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Compiling a more complicated<br />
C++ program <strong>and</strong> library<br />
Project consists of 5 files:<br />
1. utility.h -- header file defining the specifications of the utility code<br />
2. Utility.cpp – actual code <strong>for</strong> the utility code<br />
3. Print.h – header file <strong>for</strong> the <strong>printing</strong> code<br />
4. print.cpp – implementation of the <strong>printing</strong> code<br />
5. Main.cpp -- main program that uses code in utility.cpp <strong>and</strong><br />
print.cpp<br />
Plan:<br />
1. Create a library containing the code in utility.cpp <strong>and</strong> readwrite.cpp<br />
2. Link the library with the object file main.o to create the executable<br />
print.h<br />
print.h <strong>and</strong> print.cpp<br />
#include <br />
void printnumber(float t);<br />
print.cpp<br />
#inlcude “print.h”<br />
void printnumber(float t)<br />
{<br />
printf(”The number is %5.2f\n”,t);<br />
}<br />
Compling <strong>and</strong> Linking<br />
Method 1 – no libaries<br />
g++ -c utility.c produces utility.o<br />
g++ -c print.c produces print.o<br />
g++ -c main.c produces main.o<br />
g++ -o main main.o utility.o print.o –lm<br />
This yields the final executable main<br />
Note that the following one-step procedure is also possible but only<br />
useful <strong>for</strong> really small projects:<br />
g++ -o main main.cpp utility.cpp print.cpp –lm<br />
Utility.h<br />
Utility.h <strong>and</strong> Utility.cpp<br />
#include <br />
float deg2rad(float t);<br />
Utility.cpp<br />
#include “utility.h”<br />
float deg2rad(float t)<br />
{<br />
return t*M_PI/180.0;<br />
}<br />
main.cpp<br />
#include “print.h”<br />
#include “utility.h”<br />
#include “math.h”<br />
Main.cpp<br />
int main (int argc,char **argv)<br />
{<br />
float t=40.0;<br />
float trad=deg2rad(t);<br />
float costrad=cos(trad);<br />
printnumber(costrad);<br />
}<br />
Compling <strong>and</strong> Linking<br />
Method 2 – static library method<br />
g++ -c utility.c produces utility.o<br />
g++ -c print.c produces print.o<br />
ar -cr libutil.a print.o utility.o produces libutil.a<br />
(a ranlib step may also be needed)<br />
g++ -c main.c produces main.o<br />
g++ -o main -lutil –lm<br />
This yields the final executable main by linking it with the library libutil.a <strong>and</strong> the<br />
math library libm.so. The size of main is approximately equal to<br />
Size(main,o) + size(libutil.a) + overhead<br />
The executable main will run just fine even if libutil.a is deleted.