Separate compilation

Separate compilation.

intset.h (the header of IntSet):


#ifndef INTSET_H
#define INTSET_H

// Set of integers

class IntSet {
private:
  static const int N = 1024; // max cardinality
  int card; // cardinality
  int arr[N];

public:
  // makes the set empty
  void empty();

  // retruns true v is a member
  bool member(int v);

  // adds v to the set
  void add(int v);

  // removes v from the set
  void remove(int v);

  // returns the cardinality of the set
  int get_card();
};

#endif

intset.cpp (the implementation of IntSet):

#include "intset.h"

void IntSet::empty() {
  card = 0;
}

bool IntSet::member(int v) {
  for (int i = 0; i < card; i++) {
    if ( arr[i] == v ) return true;
  }
  return false;
}

void IntSet::add(int v) {
  if ( ! member(v) ) {
    arr[card] = v;
    card ++;
  }
}

void IntSet::remove(int v) {
  for (int i = 0; i < card; i++) {
    if ( arr[i] == v ) {
      // found v in arr
      for (int j = i; j < card; j++) {
        arr[j] = arr[j+1];
      }
      card --;
      break;
    }
  }
}

int IntSet::get_card() {
  return card;
}

main.cpp (the main application file):

#include <iostream>
#include "intset.h"

using namespace std;

int main () {
  IntSet s;
  s.empty();

  s.add(10);
  s.add(20);
  s.add(15);
  s.add(5);
  s.add(5);
  s.add(15);
  s.add(7);
  s.remove(15);
  
  for(int i = 0; i < 25; i++) {
    cout << i << " ";
    
    if (s.member(i)) cout << "yes";

    cout << endl;
  }
}

To build

Very simple. We compile each source code file (“.cpp”) separately with the option -c, obtaining so called object files (“.o”):

g++ -c intset.cpp
g++ -c main.cpp

After that, we link them together:

g++ -o prog intset.o main.o

And now can execute as usual:

./prog

make and Makefiles

Makefile (put these files in the same folder with your source code files)

# A simple Makefile to build the program. 
# It can be greatly improved, actually.

# The main goal is build the executable "prog"
all: prog

# To build the program prog, first two object files 
# "intset.o" and "main.o" must be built
prog: intset.o main.o 
    g++ -Wall -o prog intset.o main.o

intset.o: intset.cpp intset.h
    g++ -Wall -c intset.cpp

main.o: main.cpp intset.h
    g++ -Wall -c main.cpp


# Remove the object files and the executable "prog"
clean:
    - rm -rf *.o prog

To build the program, type make:

$ make
g++ -c intset.cpp
g++ -c main.cpp
g++ -o prog intset.o main.o

To remove the executable and the object files, type make clean:

$ make clean
rm -rf *.o prog

A much better Makefile

# A more convenient Makefile to build the program

CC = g++
CFLAGS = -Wall

# just list the object files
OBJS = main.o intset.o

all: prog

prog: $(OBJS)
    $(CC) -o $@ $(OBJS)

clean:
    - rm -rf *.o prog

Read more about writing Makefiles:

  1. http://mrbook.org/blog/tutorials/make/
  2. http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html
  3. http://www.haverford.edu/cmsc/course-pages/usingMake.html
  4. http://blog.semmy.me/post/30334566329/make-makefiles-and-c