teaching machines

CS 352 Lecture 5 – Makefiles

September 16, 2016 by . Filed under cs352, fall 2016, lectures.

Dear students,

I’ve gotten some emails about homework structure, C++, and Makefiles. I had a completely different discussion planned, but I think our time is best spent addressing your concerns. Today, then, we will create a skeletal C++ project that can be built with a Makefile.

You may want to check out a few tutorials on Makefiles. Here are a couple decent ones:

The second is an excerpt from the full manual for GNU Make, which is going to cover everything that’s possible.

See you next class!

Sincerely,

P.S. Here’s the code we wrote together in class…

Makefile

CPP = g++
CFLAGS = -g -Wall -pedantic -std=c++11

main: main.o Baseball.o
  $(CPP) $(CFLAGS) -o $@ $^

main.o Baseball.o: Baseball.h

%.o: %.cpp
  $(CPP) $(CFLAGS) -c $<

clean:
  rm -f *.o main

# main.o: main.cpp
# $(CPP) $(CFLAGS) -c main.cpp

main.cpp

#include "Football.h"
#include "Baseball.h"

int main(int argc, char **argv) {
  Baseball ball;
  ball.Message();
}

Baseball.h

#ifndef BASEBALL_H
#define BASEBALL_H

#include "Football.h"

class Baseball {
  public:
    void Message();
};

#endif

Baseball.cpp

#include <iostream>

#include "Baseball.h"

void Baseball::Message() {
  std::cout << "I don't care." << std::endl;
}

Football.h

#ifndef FOOTBALL_H
#define FOOTBALL_H

int a = 14 + 5;

#endif