My second g++/cpp program – Using a Makefile

I downloaded the example files from here:

http://www.cs.bu.edu/teaching/cpp/separate-compilation/

This demonstration explains how to compile using the make file. In this example you split up the program into separate modules, and use a make file to compile. This has the advantage that after making a change, only that part of the program needs to be recompiled.

Read the instructions on the web page, after downloading you need to edit Point.h to prevent it being included twice.

#ifndef POINT_H
#define POINT_H
/*
* File: Point.h
* Last Modified: January 31, 2000
* Topic: Modules, Separate Compilation, Using Make Files
* ----------------------------------------------------------------
*/

#include // one of two
using namespace std; // two of two, yay!

class Point
{
public:
Point();
Point(int xval, int yval);
void move(int dx, int dy);
int get_x() const;
int get_y() const;

private:
int x;
int y;
};

#endif

otherwise you will get the error:
g++ -c -o main.o main.cpp
In file included from Rectangle.h:13,
from main.cpp:9:
Point.h:13: error: redefinition of 'class Point'
Point.h:14: error: previous definition of 'class Point'

The files you download are: main.cpp, Makefile, Point.cpp, Point.h, Rectangle.cpp, Rectangle.h

and the Makefile looks like this:

# Makefile for Separate Compilation Example

# *****************************************************
# Parameters to control Makefile operation

CXX = g++
CXXFLAGS =

# ****************************************************
# Entries to bring the executable up to date

main: main.o Point.o Rectangle.o
$(CXX) $(CXXFLAGS) -o main main.o Point.o Rectangle.o

main.o: Point.h Rectangle.h

Point.o: Point.h

Rectangle.o: Rectangle.h Point.h

after running the make command you will have a bunch of object files (.o) and an executable. If you edit Rectangle.cpp and run make again then only Rectangle.o needs to be made again. The programs fragments of binary are linked by the .h header files.

if you run without preventing the multiple inclusion you get this error:

$ make
g++ -c -o main.o main.cpp
In file included from Rectangle.h:13,
from main.cpp:9:
Point.h:13: error: redefinition of ‘class Point’
Point.h:14: error: previous definition of ‘class Point’
make: *** [main.o] Error 1

After correction you get this:

$ make
g++ -c -o main.o main.cpp
g++ -c -o Point.o Point.cpp
g++ -c -o Rectangle.o Rectangle.cpp
g++ -o main main.o Point.o Rectangle.o

Edit Rectangle.cpp and run again; (e.g. open and save to update time, or just type “touch Rectangle.cpp“.


e$ make
g++ -c -o Rectangle.o Rectangle.cpp
g++ -o main main.o Point.o Rectangle.o

Advertisements

One Response

  1. Instead of the #ifdef technique, you can put this code at the beginning of your header:

    #pragma once

    It does exactly the same and most modern compilers support it.

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: