469,342 Members | 5,295 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

makefile and header files

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 file the associated cpp file is recompiled.
I tried using constructs like:

..h.cpp:
touch $<

thinking that if make detects a change in file.h it will touch file.cpp
which will then result in .cpp.o rule being executed. I tried this and some
other permutations but they all failed.

Any idea how I can fix this issue?

Thanks,
Hans.
# Specify all source
SRCS = sc_main.cpp

# Target file
TARGET = run

# Variable that points to SystemC installation path
SYSTEMC = /usr/local/systemc-2.2

INCDIR = -I. -I.. -I$(SYSTEMC)/include
LIBDIR = -L. -L.. -L$(SYSTEMC)/lib-cygwin
LIBS = -lsystemc -lm

CC = g++
CFLAGS = -g -Wno-deprecated -Wall
OBJS = $(SRCS:.cpp=.o)

EXE = $(TARGET).exe

..SUFFIXES: .cpp .o

$(EXE): $(OBJS)
$(CC) $(CFLAGS) $(INCDIR) $(LIBDIR) -o $@ $(OBJS) $(LIBS) 2>&1 | c++filt

all: $(EXE)

..cpp.o:
$(CC) $(CFLAGS) $(INCDIR) -c $<

clean:
rm -f $(OBJS) *~ $(EXE) *.bak $(TARGET).exe.stackdump

Jul 12 '07 #1
7 19165
HT-Lab a écrit :
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 file the associated cpp file is recompiled.
I tried using constructs like:

.h.cpp:
touch $<

thinking that if make detects a change in file.h it will touch file.cpp
which will then result in .cpp.o rule being executed. I tried this and some
other permutations but they all failed.

Any idea how I can fix this issue?
No but if all you need is recompilation because of header change, the
usual method is to add the rule:

monobjet.o: monheader.h myheader.h meinheader.h

Using gcc, you can automatically generate dependency files through the
-M (or -MM) flags (man gcc for more info).

Michael

Jul 12 '07 #2
On Thu, 12 Jul 2007 11:40:25 +0200, Michael DOUBEZ wrote:
HT-Lab a écrit :
>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 file the associated cpp file is
recompiled. I tried using constructs like:

.h.cpp:
touch $<

thinking that if make detects a change in file.h it will touch file.cpp
which will then result in .cpp.o rule being executed. I tried this and
some other permutations but they all failed.

Any idea how I can fix this issue?

No but if all you need is recompilation because of header change, the
usual method is to add the rule:

monobjet.o: monheader.h myheader.h meinheader.h

Using gcc, you can automatically generate dependency files through the
-M (or -MM) flags (man gcc for more info).
If it's of interest to anyone, here's a really neat 'make' idiom that
updates dependencies "on the fly" (can't remember where I found it... I
think GNU autotools does something similar):

SRCS = ...
OBJS = $(patsubst %.cpp,%.o,$(SRCS))
DEPS = $(patsubst %.o,%.d,$(OBJS))

CFLAGS = ...

....

$(OBJS): %.o: %.cpp
$(CXX) -c -MMD -MP $(CFLAGS) $< -o $@
@sed -i -e '1s,\($*\)\.o[ :]*,\1.o $*.d: ,' $*.d

-include $(DEPS)

This creates a .d dependency file for every .o file in the same pass as
compilation (the point is you only ever need updated dependencies at the
*next* build...). The odd-looking 'sed' line makes the .d file itself
depend on relevant source and headers, while the -MP adds a 'phony'
target for all prerequisite headers to stop 'make' complaining if you
remove a header. The -include stops 'make' complaining if any .d files
are not found (eg. on initial build).

It's all pretty seamless - write once and never touch anything (pun
intended) again.

--
Lionel B
Jul 12 '07 #3
HT-Lab wrote:
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 file the associated cpp file is
recompiled. I tried using constructs like:

.h.cpp:
touch $<

thinking that if make detects a change in file.h it will touch file.cpp
which will then result in .cpp.o rule being executed. I tried this and
some other permutations but they all failed.
You don't need any complex solution to be able to do this. As I understand,
he makefile structure is basically this:

<makefile>
target: files
command
</makefile>

The target command is executed if any file in the given list is modified.
So, the only thing you have to do is include the header file as one of the
target's files. That's pretty much the very basic concept behind a
makefile: define a target, define a list of files that trigger that target
and associate a series of commands to that target.

Hope this helps
Rui Maciel
Jul 12 '07 #4
HT-Lab wrote:
Not strictly a C++ question but I am sure this is a common compile
issue :-)
Nope. Many here don't even use makefiles. Resolution: add the .h
file as a dependency of your object file.
I have a generic makefile [..]
Or post to the newsgroup for your OS or your compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 12 '07 #5
On Jul 12, 6:34 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
HT-Lab wrote:
Not strictly a C++ question but I am sure this is a common compile
issue :-)
Nope. Many here don't even use makefiles.
I agree (for once) that the question really isn't on topic (and
that it would be better asked in a group for the OS), but
seriously, does anyone really develope software without using
makefiles? (The problem is, of course, that make is not always
make. Even between two different Unixes, the makefiles often
aren't compatible, and this time, Microsoft fits right in with
Unix---their make isn't compatbile with anyone elses either:-).)

--
James Kanze (Gabi Software) email: 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

Jul 12 '07 #6
James Kanze wrote:
On Jul 12, 6:34 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>HT-Lab wrote:
>>Not strictly a C++ question but I am sure this is a common compile
issue :-)
>Nope. Many here don't even use makefiles.

I agree (for once) that the question really isn't on topic (and
that it would be better asked in a group for the OS), but
seriously, does anyone really develope software without using
makefiles? (The problem is, of course, that make is not always
make. Even between two different Unixes, the makefiles often
aren't compatible, and this time, Microsoft fits right in with
Unix---their make isn't compatbile with anyone elses either:-).)
Well, they do have 'NMAKE' utility, but usually when one develops
using MS Visual Studio, they don't use NMAKE, they use the 'cl'
that knows how to deal with "dsw" ("sln") and "dsp" ("vcproj")
files, which are a sort of makefile, but not really. They just
have slightly different information in them, although the purpose
is the same, essentially.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 12 '07 #7
Thanks for all replies guys,

Regards,
Hans.

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f7**********@news.datemas.de...
James Kanze wrote:
>On Jul 12, 6:34 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>>HT-Lab wrote:
Not strictly a C++ question but I am sure this is a common compile
issue :-)
>>Nope. Many here don't even use makefiles.

I agree (for once) that the question really isn't on topic (and
that it would be better asked in a group for the OS), but
seriously, does anyone really develope software without using
makefiles? (The problem is, of course, that make is not always
make. Even between two different Unixes, the makefiles often
aren't compatible, and this time, Microsoft fits right in with
Unix---their make isn't compatbile with anyone elses either:-).)

Well, they do have 'NMAKE' utility, but usually when one develops
using MS Visual Studio, they don't use NMAKE, they use the 'cl'
that knows how to deal with "dsw" ("sln") and "dsp" ("vcproj")
files, which are a sort of makefile, but not really. They just
have slightly different information in them, although the purpose
is the same, essentially.

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

Jul 13 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Matthias Pieroth | last post: by
1 post views Thread by MPD | last post: by
4 posts views Thread by Sharp Tool | last post: by
2 posts views Thread by Juhan Voolaid | last post: by
5 posts views Thread by Jacobo Rodriguez Villar | last post: by
4 posts views Thread by Jess | last post: by
2 posts views Thread by Aditya | last post: by
2 posts views Thread by g.vukoman | last post: by
5 posts views Thread by M.Liang Liu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.