tags:

views:

122

answers:

5

I have a bunch of files in different folders:

/ai/client.cpp   # contains the main function

/ai/utils/geometry.h   
/ai/utils/geometry.cpp

/ai/world/world.h
/ai/world/world.cpp
/ai/world/ball.h
/ai/world/ball.cpp
/ai/world/bat.h
/ai/world/bat.cpp

How do I write a makefile to compile this program? I'm using Ubuntu.

A: 
  1. you should check out that you have installed g++ and build-essential
  2. here is some insight into the makefile black magic consorsium
  3. I think that make1 is directory aware so typing mydirectory/myfile.cpp should work well
  4. the rest is basic g++ commands but the tutorial on 1 should be enough :)

1 the program that executes makefiles

phmr
it was a good link!
phmr
lmgtfy .com is a prohibited link because it is considered offensive. Hiding it behind a URL shortener is twice as bad. Don't do that. Your updated link is much better.
Roger Pate
+2  A: 

Make is a versatile tool, and there are many different subtleties to using it. However, you can keep things simple:

OBJ := ai/utils/geometry.o ai/world/world.o ai/world/ball.o ai/world/bat.o

all: ai/client
.PHONY: all  # specific to GNU make, which is what Ubuntu provides

ai/client: ai/client.o $OBJ

# this rule means each .cpp file depends on its corresponding header
# and, since the .o files depend on .cpp files (a builtin make rule),
# they will be recompiled if the headers change
#
# you can also get more complex and generate dependencies automatically
# look at the -MM option for gcc, for example
%.cpp: %.h
Roger Pate
There is one caveat which I can't remember if it applies in this case: if your make's default is to use CC to link (which probably means the gcc command), you might need `CC := g++` in the makefile to link against the C++ stdlib (or something similar, but this works well enough if you have no C programs compiled by this makefile).
Roger Pate
@Roger: GNU make should use `$(CXX)` by default for compiling c++ code, and *that* should default to `g++`. *Don't* tell it to use `g++` for the c compiler!
dmckee
@dmckee: Compiling, yes, but the issue is *linking*. On my Ubuntu VM, make uses (as seen through `make -p`) CC to link all *.o files, regardless of whether they came from C or C++ source files. Is there a configuration setting I'm missing?
Roger Pate
@Roger: Uh....uh....I had never noticed that before. I habitually provide an explicit link rule, so it had never come up. I'm just stonkered. I think you'd better disregard the above hysterical ranting. *I* am going to have a beer.
dmckee
A: 

First result in google: http://www.opussoftware.com/tutorial/TutMakefile.htm

Seems to be a pretty good tutorial. Should be pretty simple to understand, note that they talk about the GNU version of make, which is what is most commonly used. There is also the BSD version though if you use a BSD-based OS(such as OpenBSD, NetBSD, or FreeBSD.. anyone know about Mac OSX?)

Earlz
So what you're saying is that you don't actually know the answer, but are mindlessly parroting Google and *hoping* the result you found is correct. How does that help the OP?
jalf
If by you mean I'm not going to give them a makefile all coded and ready for them to copy and paste, then yes. I've reviewed that tutorial, and it's good. I'm not going to try to summarize it because make is too complex to explain in one of these little answers. I wish for the asker to understand the code given, not to just blindly copy and paste.
Earlz
@Earlz: While I agree with you on the rest, a real working example that he can copy and paste is a great addition to a good tutorial (and the best tutorials include working examples themselves). Being able to modify and try out something as you read is central to how some people learn.
Roger Pate
A: 

I wrote this but its still not working

CC = g++
DEBUG = -g
FLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
prat: client.o world.o robot.o football.o geometry.o
$(CC) $(LFLAGS) $(OBJS) -o prat
client.o: client.cpp world/world.h
$(CC) $(CFLAGS) client.cpp
world.o: world/world.h world/world.cpp world/robot.h world/football.h utils/geometry.h
$(CC) $(CFLAGS) world/world.cpp
robot.o: world/robot.h world/robot.cpp utils/geometry.h
$(CC) $(CFLAGS) world/robot.cpp
football.o: world/football.h world/football.cpp utils/geometry.h
$(CC) $(CFLAGS) world/football.cpp
geometry.o: utils/geometry.h utils/geometry.h
$(CC) $(CFLAGS) utils/geometry.cpp
clean:
\rm *.o *~ prat

and the tabs are there

Update the question instead of posting a response as an answer. You should not be repeating rules that make has built-in. Did you try my answer? My answer (plus first comment if I remembered correctly) should work exactly as written.
Roger Pate
And when you do update to move this into the question, be very specific about how it's still not working, e.g. error messages, etc.
Roger Pate
A: 

its working thank you every1 for your valuable comments specially for the links on the previous post i forgot to write the client.cpp file on line 6 but my mistake was that i had included one header with a mistake in the client.cpp and it could never find it.