LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2005, 05:53 AM   #1
Meaculpa
LQ Newbie
 
Registered: Mar 2004
Location: Germany
Distribution: Mandrake Linux LE 2005 (x86_64 and i586)
Posts: 3

Rep: Reputation: 0
Makefile and make depend


Hi folks,

I am trying to create a standardized Makefile construct for my projects. Standardized means i do not need to specify the targets all one by one but with pattern matching like
Code:
%.o: %.cc %.h
    $(CXX) ...
Now there comes up a problem: If i got a headerfile named equal as the sourcefile all works right. But if not make will not find a target to compile.
Example:
Code:
...
SOURCES:=main.cc test.cc
HEADERS:=test.h
TARGETDIR:=../objects
 ...

$(TARGETDIR)/%.o: %.cc %.h
    $(CXX) $(CXXFLAGS) -o $@ $<

depend:
    $(CXX) -E -MM $(SOURCES) $(HEADERS) > .depend
This snippet will compile test.cc if test.h is changed. But it will not compile main.cc because there is no header.

Now the question: How can I modify the makefile so that it can check if a header exists and than and only than include it to the rule?

I am grateful for any suggestions

Greetz Mea
 
Old 06-30-2005, 03:19 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I typically do something like this:

Code:
SOURCES:=test.cc

%.o: %.cc *.h
        $(CXX) $(CXXFLAGS) -o $@ $<

depend:
        $(CXX) -E -MM *.cc > .depend
My typical directory layout will have a subdir for each module of the project. I never have header files inside the subdir that wouldn't be depended on which is why I feel I can safely use *.h. As far as the g++ -MM is conserned... it gets the dependencies based on the includes of a file so it isn't neccesary to have the $(HEADERS) part.
 
Old 07-01-2005, 07:54 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Try this as a start. Simple makefile for multiple source file targets.
Utilises make's in-built rules where possible.

Single directory.
Compile multiple .c .h files into one target.

Just change the target name at the top.
Automagically picks up new source files.


I hope.

Code:
TARGET=blah_blah
#	---------------------------------
#	Standard makefile template
#	just specify the target name above
#	and the make file should sort it
#	all out (hopefully)
#	---------------------------------

INCLUDEFILE = include.mk


# this should appear above the 'include' directive below
# calls for the target and then the cleanup
# =========================================
all: ${TARGET} cleanup 


-include ${INCLUDEFILE}

# ${INCLUDEFILE} should not exist when it's included.
# As it does not exist it is treated as a target.
# Therefore it is created anew each time
# by the appropriate rule, below
# Which finds .c and .h files and calls makedepend for them
# =========================================================

${INCLUDEFILE}:
	@echo "### creating ${INCLUDEFILE}"
	@echo "### creating ${INCLUDEFILE}" > ${INCLUDEFILE}
	-@ echo  *.c|xargs echo SRCFILES = >> ${INCLUDEFILE}
	-@ echo  *.h|xargs echo HFILES = >> ${INCLUDEFILE}
	@  makedepend  -f${INCLUDEFILE} *.c


# (SRCFILES defined in INCLUDEFILE)
# =================================
OBJFILES = ${SRCFILES:.c=.o}


# this is the working target
# ==========================

${TARGET}: ${OBJFILES} 
	${CC} -o $@ ${OBJFILES}

help: print_help cleanup

print_help:
	echo  OBJFILES = ${OBJFILES}
	echo  SRCFILES = ${SRCFILES}
	cat ${INCLUDEFILE}
	

clean:cleanup
	-@ rm -f  *.o 

# we need to delete the INCLUDEFILE otherwise
# new source and header files won't be picked up
# next time
# =========
cleanup:
	@echo "### deleting ${INCLUDEFILE}"
	-@ rm -f  ${INCLUDEFILE}

Last edited by bigearsbilly; 07-01-2005 at 08:06 AM.
 
Old 03-06-2009, 05:33 AM   #4
Mark101
LQ Newbie
 
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
I have some trouble with making dependencies.

My makefile concerning build and depend rules look something like:

...
${OBJDIR}/%.o : %.c
gcc <flags> ${INCPATH} -o ${@} $<

depend:
gcc -E -MM ${INCPATH} *.c > ${OBJDIR}/.depend

include ${OBJDIR}/.depend
...

I want to compile my sources and put the objects in a separate directory (= ${OBJDIR} == ../obj/tsm). That works fine. However, I have some trouble making use of the dependency file. When making the depend file the output looks like:

tsm_file.o: tsm_file.c tsm_defs.h tsm_type.h tsm_exts.h
tsm_init.o: tsm_init.c tsm_defs.h tsm_type.h tsm_args.h

But I want it to look like below in order to use it:
../obj/tsm/tsm_file.o: tsm_file.c tsm_defs.h tsm_type.h tsm_exts.h
../obj/tsm/tsm_init.o: tsm_init.c tsm_defs.h tsm_type.h tsm_args.h

How can I adjust my depend rule to accomplish this?

Best regards,
Mark
 
Old 03-06-2009, 11:24 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
This link provides different approach about this subject (GNU-make specific though):

http://www.gnu.org/software/make/man...-Prerequisites
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do we make MakeFile...? asahlot Programming 1 10-17-2005 02:25 PM
How to use make? and makefile? markw8500 Linux - Newbie 11 08-08-2005 10:21 PM
WineX Make Depend Errors Gryphix Linux - Games 4 09-25-2004 04:36 PM
how 2 make a makefile? beginner_84 Programming 6 08-29-2004 02:47 PM
Error in Make Depend while CVS install of WineX calmlikeabomb Mandriva 1 04-28-2004 05:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration