473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Makefile question.

xz
I am a rookie of C++ and got so confused with the Makefile these days.
Could anyone be so kind and give a little sample Makefile for the
following particular example?

Let's say I have the following files describing 4 classes (A, B, C1,
C2) and a test program (test.cpp) which runs a test for classes C1 and
C2.

A.h
A.cpp
B.h
B.cpp
C1.h
C1.cpp
C2.h
C2.cpp
test.cpp

The inhirentance relation among the 4 classes are as follows:

class C1: public B
class C2: public B
class B: public A

i.e. C1 and C1 are both dependent on B while B is dependent on A.

test.cpp depends on C1 and C2 and contains the main() function.

I am on a linux machine(Ubuntu 7.10) and use g++ 4.1 as the compiler.

Then how should I write the Makefile? Thanks a lot in advance!
Jan 9 '08 #1
8 3179
xz wrote:
I am a rookie of C++ and got so confused with the Makefile these days.
Those have really nothing to do with each other. C++ language
Standard does not define 'Makefile', neither is 'Makefile' C++
specific.
Could anyone be so kind and give a little sample Makefile for the
following particular example?
[..]
I am on a linux machine(Ubuntu 7.10) and use g++ 4.1 as the compiler.

Then how should I write the Makefile? Thanks a lot in advance!
Go to 'gnu.*' or 'comp.os.linux.*' hierarchies.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 9 '08 #2
This might work (or might not). Makefiles are fiddly.

Where you see <tabin the following lines, please
remove the characters "<tab>" and insert the actual
tab key from the keyboard. Makefiles use the tab key
for executable lines.

#%<----------------------------------
SRC= test.cpp C1.cpp C2.cpp B.cpp A.cpp
OBJ= $(SRC:.cpp=.o)
EXE= test
CC= g++
%.o: %.cpp
<tab>$(CC) -o $@ -c $<
..PHONY: all
all: $(EXE)
$(EXE): $(OBJ)
<tab>$CC $(OBJ) -o $(EXE)
xz wrote:
I am a rookie of C++ and got so confused with the Makefile these days.
Could anyone be so kind and give a little sample Makefile for the
following particular example?

Let's say I have the following files describing 4 classes (A, B, C1,
C2) and a test program (test.cpp) which runs a test for classes C1 and
C2.

A.h
A.cpp
B.h
B.cpp
C1.h
C1.cpp
C2.h
C2.cpp
test.cpp

The inhirentance relation among the 4 classes are as follows:

class C1: public B
class C2: public B
class B: public A

i.e. C1 and C1 are both dependent on B while B is dependent on A.

test.cpp depends on C1 and C2 and contains the main() function.

I am on a linux machine(Ubuntu 7.10) and use g++ 4.1 as the compiler.

Then how should I write the Makefile? Thanks a lot in advance!
Jan 10 '08 #3
x-no-archive: yes
This might work (or might not). Makefiles are fiddly.

Where you see <tabin the following lines, please
remove the characters "<tab>" and insert the actual
tab key from the keyboard. Makefiles use the tab key
for executable lines.

#%<----------------------------------
SRC= test.cpp C1.cpp C2.cpp B.cpp A.cpp
OBJ= $(SRC:.cpp=.o)
EXE= test
CC= g++
%.o: %.cpp
<tab>$(CC) -o $@ -c $<
..PHONY: all
all: $(EXE)
$(EXE): $(OBJ)
<tab>$CC $(OBJ) -o $(EXE)
xz wrote:
I am a rookie of C++ and got so confused with the Makefile these days.
Could anyone be so kind and give a little sample Makefile for the
following particular example?

Let's say I have the following files describing 4 classes (A, B, C1,
C2) and a test program (test.cpp) which runs a test for classes C1 and
C2.

A.h
A.cpp
B.h
B.cpp
C1.h
C1.cpp
C2.h
C2.cpp
test.cpp

The inhirentance relation among the 4 classes are as follows:

class C1: public B
class C2: public B
class B: public A

i.e. C1 and C1 are both dependent on B while B is dependent on A.

test.cpp depends on C1 and C2 and contains the main() function.

I am on a linux machine(Ubuntu 7.10) and use g++ 4.1 as the compiler.

Then how should I write the Makefile? Thanks a lot in advance!
Jan 10 '08 #4
On Jan 10, 12:50 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
xz wrote:
I am a rookie of C++ and got so confused with the Makefile these days.
Those have really nothing to do with each other. C++ language
Standard does not define 'Makefile', neither is 'Makefile' C++
specific.
Could anyone be so kind and give a little sample Makefile for the
following particular example?
[..]
I am on a linux machine(Ubuntu 7.10) and use g++ 4.1 as the compiler.
Then how should I write the Makefile? Thanks a lot in advance!
Go to 'gnu.*' or 'comp.os.linux.*' hierarchies.
Linux make is GNU make, and I believe that there's a special
mailing list for GNU make. Still, my advice would be:

-- If you're in a professional environment, use their standard
build tools, and don't worry about it. I've never seen a
standard environment where programmers had to write more
than a couple of lines of make, and those generally didn't
look much like a standard makefile either.

-- If you're working on your own, trying to learn C++, get an
IDE, and let it generate the makefiles for you. While the
IDE's that I've seen are pretty worthless (in fact, they're
rather counter-productive) for professional programming,
they're ideal for learning, and they allow you to
concentrate on one thing at a time: C++, for example, rather
than makefiles.

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jan 10 '08 #5
If you are interested in making 'make' just work, without having to
know why it works, try this generic Makefile which I myself usually use
for everything:
# List source files (not headers!) and binary name:
# Use:
#SOURCES=file1.cc file2.cc file3.cc
# or:
SOURCES=$(wildcard *.cc)

BINARY=programname

# Set up compiler and compiler options:
CXX=g++
CXXFLAGS=-Wall -W -ansi -O3
LDLIBS=

# If you are compiling a C program (instead of C++), comment out
# this line:
LINK.o=$(LINK.cpp)
# --- No need to modify anything below this line ---
$(BINARY): $(SOURCES:.cc=.o)

..dep:
g++ -MM $(SOURCES) .dep

-include .dep
Jan 10 '08 #6
Juha Nieminen wrote:
# List source files (not headers!) and binary name:
# Use:
#SOURCES=file1.cc file2.cc file3.cc
# or:
SOURCES=$(wildcard *.cc)

BINARY=programname

# Set up compiler and compiler options:
CXX=g++
CXXFLAGS=-Wall -W -ansi -O3
LDLIBS=

# If you are compiling a C program (instead of C++), comment out
# this line:
LINK.o=$(LINK.cpp)
# --- No need to modify anything below this line ---
$(BINARY): $(SOURCES:.cc=.o)

.dep:
g++ -MM $(SOURCES) .dep

-include .dep
Forgot to tell: It can't know if your #include lines change in your
source files, so if you change them you'll have to "rm .dep" before
executing "make" so that it can update the dependencies.
Jan 11 '08 #7
xz
I understand the most simple (but kinda silly) way to write a Makefile
is to simply put all related the cpp files after the colon and compile
them all, like this:

testprogram: A.cpp B.cpp C1.cpp C2.cpp test.cpp
<Tab>g++ $? -o $@

However, the wierd thing that I came across was:
Every time I run "make testprogram" for the first time, the compiler (g
++) complaines that it cannot find the definitions of some member
functions, which I have defined in A.cpp or B.cpp

(Of course I hvae included C1.h and C2.h in test.cpp, included B.h in
C1.h, and included A.h in B.h.)

And, if I run "make testprogram" again (for the second time), then
they all compile well and the executable is just made there in good
state.

Anybody knows why that is happening?
I am using emacs and the running the compile command "make
testprogram" within emacs, I am not sure if that matters.
Jan 14 '08 #8
xz wrote:
I understand the most simple (but kinda silly) way to write a Makefile
is to [.. problems using 'g++' and 'make' ..]

Anybody knows why that is happening?
I am using emacs and the running the compile command "make
testprogram" within emacs, I am not sure if that matters.

Somebody knows. But this is the wrong newsgroup to ask about it.
Please try 'gnu.*' hierarchy or the newsgroup for your platform.
'make' or 'emacs' are not part of C++ _language_ and as such are
off-topic here. 'g++' has its own newsgroup.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 14 '08 #9

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

Similar topics

0
by: John Abel | last post by:
Hi, I've a small script, which listens to a port on a remote machine. I'm trying to implement makefile. However, the code on Win32 Py 2.2.2 def _Connect( self ): sockConn = socket.socket(...
4
by: Efrat Regev | last post by:
Hello, I'd like to ask a question concerning a python script in a makefile. Suppose I have a C++ project (sorry for raising this in a Python newsgroup), with some makefile for it. Before...
3
by: Faith | last post by:
any good tips on reading makefiles that were written using C++ ASAP please
4
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...
1
by: Carson | last post by:
Hi, I have a problem with Makefile. I have a piece of c code, which is stored in my unix account. I wrote a Makefile for that particular compilation (using gcc), and it works fine. But...
4
by: srikar | last post by:
Hi can any one explain me the reason. For any Makefile there will be assosiated rules file, may be in same directory or in other directory. What is the need for this rules file. Is it that...
4
by: Jess | last post by:
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
9
by: raju2000myin | last post by:
i wrote one makefile but it shows multiple definition error how can i resolve that problem it is urgent to me please help to me............. ramesh............
7
by: HT-Lab | last post by:
Hi All, Not strictly a C++ question but I am sure this is a common compile issue :-) I have a generic makefile (see below) that I would like update such that if I make some changes to a header...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.