0

I'm trying to create a makefile that will compile two files: -main.cpp -queue.h Inside of the header file I have the a full implementation of a template Queue class. The main file includes the header file.

I can test this code in the terminal (on a Mac) by typing this in:

g++ -c main.cpp
g++ -o queue main.o

and I'm able to run the program (./queue) with no problem.

Now for this project it needs to compile by just typing "make" in the terminal. For previous projects I used something like this:

all: sDeque

sDeque: sDeque.o sDeque_main.o
    g++ -o sDeque sDeque.o sDeque_main.o

sDeque.o: sDeque.cpp sDeque.h
    g++ -c sDeque.cpp

sDeque_main.o: sDeque_main.cpp sDeque.h
    g++ -c sDeque_main.cpp
clean:
    rm sDeque sDeque.o sDeque_main.o

But the example above has a main.cpp , sDeque.cpp and sDeque.h. And this works since I can create object files but I don't think I can create an object file from just a header file.

For the task at hand I only have the main and the header file (since its a template class). How do I make the makefile for this?

GusGus
  • 230
  • 6
  • 16
  • 1
    Why are you writing your own Makefiles? Why not use something like cmake or qmake that generates Makefiles for a plethora of different platforms? – Kenny Worden Feb 22 '15 at 19:49
  • I need to include the makefile for the project, the professor is going to use his own main to test my template class and need to have a generic makefile for him. – GusGus Feb 22 '15 at 19:51
  • You could still use cmake/qmake to generate the Makefile to ship off with your project. It's a lot easier to write and manage cmake/qmake files when your project grows, anyhow. – Kenny Worden Feb 22 '15 at 19:52
  • Actually, you _can_ create an object file from a header file, if there's a valid implementation in the file, but it's generally not good practice to put implementation in a header. That being said, if the prof is going to use his own main.cpp and you have a header you're providing, you can simply make a series of targets to make "main", consisting of the header, main.o and main.cpp files that he should be able to use, simply by extending your example in the post. – Ryan J Feb 22 '15 at 19:53
  • and how exactly do I use cmake/qmake? I'm new to the world of makefiles lol – GusGus Feb 22 '15 at 19:53
  • 1
    @GusGus I actually disagree with Kenny. If you're just starting out, then you should understand the basics of make. I would suggest heading over to http://mindview.net/Books/TICPP/ThinkingInCPP2e.html and reading the end of chapter 3 of volume 1. Its a nice introduction and you can download for free. – Freddy Feb 22 '15 at 19:57
  • Thank you Freddy! I will certainly take a look at that. – GusGus Feb 22 '15 at 20:06

2 Answers2

2
# All the targets
all: queue

# Dependencies and rule to make queue
queue: main.o
    g++ -o queue main.o

# Dependencies and rule to make main.o
main.o: main.cpp queue.h
    g++ -c main.cpp

clean:
    rm queue main.o
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • This works perfectly! thank you so much. I wish i could upvote but I'm not of enough rep yet I guess? – GusGus Feb 22 '15 at 20:05
  • @GusGus, no worries. When you have time, you should get to know [GNU make manual](http://www.gnu.org/software/make/manual/make.html). It has a lot of tricks up its sleeve. – R Sahu Feb 22 '15 at 20:11
0

R Sahu's answer is correct, but note that you can do a lot better (less typing) by taking advantage of GNU make's built-in rules, which already know how to do this stuff.

In particular, if you rename your main.cpp file to queue.cpp (so that it has the same prefix as the program you want to generate, queue), your entire makefile can consist of just:

CXX = g++

all: queue
queue.o: queue.h

clean:
        rm queue queue.o
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Except on Windows under MSYS, where implicit rules take _forever_ and you're best off (a) disabling them with `-r` and (b) writing the rules you need yourself, as in the other answer. – Lightness Races in Orbit Feb 23 '15 at 01:23
  • Even on Windows this simple makefile will not be annoyingly slow. If it got significantly more complex then yes, you may need to do something about unneeded built-in rules. – MadScientist Feb 23 '15 at 13:33