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.

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

#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);}


#include “kelas.h”
void cPolygon::set_values (int a, int b) {

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

//main class

#include “kelas.h”
using namespace std;

int main() {
cRectangle rect;
cTriangle trgl;
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



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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: