Connecting Tech Pros Worldwide Forums | Help | Site Map

makefile help

Jess
Guest
 
Posts: n/a
#1: Apr 25 '07
Hello,

I am now trying to use makefile to compile C++ programs. My makefile
looks like:

f1.o : f1.cpp h1.h h2.h
g++ -c f1.cpp

f2.o : f2.cpp h2.h h3.h
g++ -c f2.cpp

final.o : final.cpp h4.h h5.h
g++ -c final.cpp

final : f1.o f2.o final.o
g++ f1.o f2.o final.o -o final

In other words, to make a ".o" file, I put its corresponding ".cpp"
and all the "included" ".h" files as dependent files. For the
executable file ("final" above), I put all the ".o" files as dependent
files, without any other ".h" files. Is this the correct approach?
Or, is there a simpler way? Additionally, is there anything important
missing from my makefile?

Thanks a lot!


Bjoern Doebel
Guest
 
Posts: n/a
#2: Apr 25 '07

re: makefile help


Hi,
Quote:
I am now trying to use makefile to compile C++ programs. My makefile
looks like:
>
f1.o : f1.cpp h1.h h2.h
g++ -c f1.cpp
>
f2.o : f2.cpp h2.h h3.h
g++ -c f2.cpp
>
final.o : final.cpp h4.h h5.h
g++ -c final.cpp
>
final : f1.o f2.o final.o
g++ f1.o f2.o final.o -o final
>
In other words, to make a ".o" file, I put its corresponding ".cpp"
and all the "included" ".h" files as dependent files. For the
executable file ("final" above), I put all the ".o" files as dependent
files, without any other ".h" files. Is this the correct approach?
Or, is there a simpler way? Additionally, is there anything important
missing from my makefile?
I'm quite sure that this question does not belong to a C++ newsgroup, anyway:

For writing a simpler Makefile you should give the GNU Make manual a read.
You may especially want to search for the chapter called "Generating
Prerequisites Automatically".

Alternatively, I suggest you try other build systems such as SCons
(www.scons.org) which do this automatically for you. The SConstruct for
your above example would be something like:

Program('final', Split("f1.cpp f2.cpp final.cpp")

Cheers,
Bjoern
Gianni Mariani
Guest
 
Posts: n/a
#3: Apr 25 '07

re: makefile help


Jess wrote:
Quote:
Hello,
>
I am now trying to use makefile to compile C++ programs. My makefile
looks like:
>
f1.o : f1.cpp h1.h h2.h
g++ -c f1.cpp
>
f2.o : f2.cpp h2.h h3.h
g++ -c f2.cpp
>
final.o : final.cpp h4.h h5.h
g++ -c final.cpp
>
final : f1.o f2.o final.o
g++ f1.o f2.o final.o -o final
>
In other words, to make a ".o" file, I put its corresponding ".cpp"
and all the "included" ".h" files as dependent files. For the
executable file ("final" above), I put all the ".o" files as dependent
files, without any other ".h" files. Is this the correct approach?
Or, is there a simpler way? Additionally, is there anything important
missing from my makefile?
>
Thanks a lot!
>
This is off topic here. Try comp.unix.programmer. I have set followups
to that NG.

Just as a suggestion, try MakeXS (the website is down, I need to get it
back up but you can get a version of you download austria C++ from
sourceforge.

For MakeXS, simply place your cpp files in a folder, under the folder
containging the MakeXS folder. Add a Makefile.xsi (exact copy of all
other Makefile.xsi files) and a Makefile that includes your Makefile.xsi
and run "make".

It automatically creates your dependantcies, i.e. "f1.o : f1.cpp
h1.h..." and has a large number of other things you can do.
Documentation is there.

Yes, I need to put MakeXS.com back up....

While I am at it, it will also create your include directory list
automatically so it allows you to separate your app with NO modification
of your Makefiles. It also builds


The latest latest version is available in the austria C++ alpha at:

http://netcabletv.org/public_releases/ (100 meg download - has lots of
precompiled binaries). MakeXS by itself is quite small.


Basically, what I am trying to say is that basic Makefiles become a
maintenance nightmare, even for small projects and somthing like MakeXS
can eliminate alot of hard work.

Peter Karlsson
Guest
 
Posts: n/a
#4: Apr 25 '07

re: makefile help


On 2007-04-25, Jess <wdfcj@hotmail.comwrote:
Quote:
Hello,
>
I am now trying to use makefile to compile C++ programs. My makefile
looks like:
>
f1.o : f1.cpp h1.h h2.h
g++ -c f1.cpp
[snip]

Here's a simple one:

----------------------------------

EXECUTABLE=filename
CXX=g++
CXXFLAGS=-W -Wall -ansi -pedantic -O2 -s -c
LDFLAGS=-O2 -s

OBJECTS=file1.o file2.o fileN.o

..PHONY: all

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
@echo "Linking $(EXECUTABLE) ..."
@$(CXX) $(LDFLAGS) $(OBJECTS) -o $@
@echo "Done."

%.o: %.cpp %.h
@echo "Compiling $@ ..."
@$(CXX) $(CXXFLAGS) $< -o $@

----------------------------------

HTH,
Peter
Jess
Guest
 
Posts: n/a
#5: Apr 26 '07

re: makefile help


Thanks to all your information!

Closed Thread