473,239 Members | 1,573 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,239 software developers and data experts.

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 19805
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
by: Matthias Pieroth | last post by:
Hi NG, I have the following MakeFile: # Compiler #--------------------------------------- CC = g++ APP = snake-client
1
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
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
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
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
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
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
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
by: M.Liang Liu | last post by:
I have a project with the following dirs: --------------------------------------------------------------------------------------- +src |-proj0 |-program1 |-program2 |-proj1 |-program1...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.