开发者

Using make for my program

开发者 https://www.devze.com 2022-12-21 02:10 出处:网络
I have a bunch of fil开发者_开发知识库es in different folders: /ai/client.cpp# contains the main function

I have a bunch of fil开发者_开发知识库es 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.


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


  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


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.


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?)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号