jump to navigation

How to compile many files of c program using g++ November 18, 2013

Posted by aghus in Bebas, Linux, Tips n Trik.
trackback

We have several files .cc and .h, e.g., kelas.h, kelas.cc, and kelasutama.cc, as follows.

———————————————-
//kelas.h
#ifndef kelas_h
#define kelas_h

//class definition
class cPolygon{
protected : int width,height;
public : void set_values(int, int);
};

class cRectangle: public cPolygon{
public : int area ();
};

class cTriangle: public cPolygon {
public : int area () {return (width*height/2);}
};

#endif
———————————————–

———————————————–
//kelas.cc
#include “kelas.h”
void cPolygon::set_values (int a, int b) {
width=a;
height=b;
};

int cRectangle::area () {
return (width*height);
};
————————————————

————————————————
//kelasutama.cc
//main class

#include “kelas.h”
#include
using namespace std;

int main() {
cRectangle rect;
cTriangle trgl;
rect.set_values(14,5);
trgl.set_values(14,5);
cout <<rect.area()<<endl;
cout <<trgl.area()<<endl;
return 0;
}
————————————————-

If we want to obtain the executable file, we can compile those files using g++.

$ g++ -Wall -g kelas.cc kelasutama.cc -lm -o kelasutama

“-g” tells the compiler to include support for the debugger
“-lm” to refer to the math library
“-o” produces a separate “object code” file (ending with .o)

Afterward, we have an executable file, which is named kelasutama.

In Linux we can run, ./kelasutama

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: