473,488 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 19817
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1702
by: Matthias Pieroth | last post by:
Hi NG, I have the following MakeFile: # Compiler #--------------------------------------- CC = g++ APP = snake-client
1
3084
by: MPD | last post by:
Hi, I'm new to the MPW (Mac Programmers Workshop -- Apple's now freely avilable development tools for Classic Mac OS's) and wish to construct a simple 2D game using MPW's C++ compiler and SDL. ...
4
2887
by: Sharp Tool | last post by:
Hi I have downloaded a program that is written in C++ that I would like to compile into a Win XP executable. The program contains a Makefile written for Unix/linux/cygwin. I would like to...
2
3001
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...
5
11554
by: Jacobo Rodriguez Villar | last post by:
Hello, Is there any way (or program) to convert and maintain easily a C++ project (vs7.1) to a Makefile file, in order to build it in Linux? Many thanks -- Jacobo Rodríguez Villar ...
4
3937
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
2
485
by: Aditya | last post by:
Hi I am unable to write a makefile in this particular case. I have a project in a folder named 'TestFile'. The 'TestFile' has two subfolders 'src' and 'header'. The folder 'src' contains...
2
2386
by: g.vukoman | last post by:
Hi all! I have a problem with my Makefile/library. My library "libmylib.so" includes the header file <boost/regex.hpp>. My Makefile uses following variables: CC = gcc CXX = g++
5
3290
by: M.Liang Liu | last post by:
I have a project with the following dirs: --------------------------------------------------------------------------------------- +src |-proj0 |-program1 |-program2 |-proj1 |-program1...
0
6967
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...
0
7142
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,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5445
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,...
1
4875
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...
0
3078
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.