473,786 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

makefile help

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!

Apr 25 '07 #1
4 3959
Hi,
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
Apr 25 '07 #2
Jess wrote:
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.progr ammer. 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.

Apr 25 '07 #3
On 2007-04-25, Jess <wd***@hotmail. comwrote:
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=file name
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
Apr 25 '07 #4
Thanks to all your information!

Apr 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4655
by: Jamie Saker | last post by:
I think I'm overlooking something assumed in socket's makefile method. Googling several hours and digging thru the python reference didn't help - I think I'm overlooking an assumption between Python and UNIX socket objects neither is explicitely discussing. I think my mknod In the makefile operation on socket (pydoc socket.socket.makefile... using AF_UNIX, allowing you to create a file object to correspond to a socket) I've got an sample...
3
1933
by: Faith | last post by:
any good tips on reading makefiles that were written using C++ ASAP please
3
4083
by: tmponko | last post by:
OK...I'm new to this and I'm sure I'm missing something obvious, so please be kind. I've tried several different approaches to this problem (including various if, ifeq, and case constructs), and I'm about to give up. Can any tell me why this doesn't work? In a nutshell, I want a Makefile that will "include" either a Linux.mk or a SunOS.mk without having to rely on an explicit command line parameter or env var. Here is the segment that...
4
3284
by: newbiecpp | last post by:
I am very sorry to post this one because I know it is out of topic but I cannot find the proper news group. I understand that most senior C++ programmers know makefile very well. My question is GNU makefile support command line argument? I want to write a makefile so that when you invoke "make test", the makefile will compile test like this: $project = "command line argument, i.e., test";
2
3021
by: Juhan Voolaid | last post by:
Hello I need help with my makefile, so that when I compile my project the source code files would be separated from the object (*.o) files. So if I have: main.cpp and classes.cpp - the filestructure should be like that: project_dir/ source_dir/ main.cpp
6
1903
by: dolphin | last post by:
Hi! I am learing makefile recently.Now I have a problem. I write a very simple program just like "hello world" I write two makefile for it.One is included in another. the first is makefile include mainmake.mk example:main.o cc -o example main.o clean: rm -f *.o
1
5045
by: rpjanaka | last post by:
I am using an open source library called IGI_UDP for measure the available bandwidth of a link (http://www.cs.cmu.edu/%7Ehnn/igi/ ). with that library they have provided a "Makefile" which is not an auto generated one. the following is the given Makefile, ***************************************************************** CC = gcc INCS = -I. CFLAGS = -g -Wall $(DEFS) $(INCS)
5
3304
by: M.Liang Liu | last post by:
I have a project with the following dirs: --------------------------------------------------------------------------------------- +src |-proj0 |-program1 |-program2 |-proj1 |-program1 |-program2 |-program3
2
2146
by: chutsu | last post by:
I'm basically a summer student working on a program called Rivet. (http://projects.hepforge.org/rivet/) And I'm trying to port it to BOINC so that the hosts can download the BOINC client and do some calculation over a large clusters of computers. Me being an absolute beginner at programming, I don't know how to update the Makefile in Rivet! I edited the Makefile.am in the src directory to include the BOINC APIs and Libraries, but what do...
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.