LinuxQuestions.org
Help answer threads with 0 replies.
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 03-28-2002, 03:51 AM   #1
whepin
Member
 
Registered: Dec 2001
Location: thunder bay, on, Canada
Distribution: Windows vista & Ubuntu
Posts: 113

Rep: Reputation: 15
About makefile


This is my makefile(test.mak):


CC = gcc
INCDIR = ../file1/inc -I ../file2/inc
C_COMPILE_CONTROL = -g
EXEDIR = ../exe/
EXENAME = test
EXEFILE = $(EXEDIR)$(EXENAME)
OBJDIR = ../obj





#------------------------------LINKER---------------------------------#
#It just creates an executable file final.out in dir EXEDIR
#The next line can be commented.

all:$(EXEFILE)
$(EXEFILE):$(OBJDIR)/test.o \
$(OBJDIR)/try.o

$(CC) $^ -o $@ -I$(INCDIR) $(C_COMPILE_CONTROL)




#------------------------COMPILER/ASSEMBLER-----------------------------#
$(OBJDIR)/test.o:../file1/test.c \
../file1/inc/test.h \
../file1/inc/test1.h \
../file1/inc/test2.h

$(CC) -c ../file1/test.c -o $@ -I$(INCDIR) $(C_COMPILE_CONTROL)

#NOTE:$@ = try.o,$^=try.c try.h,HERE can NOT use $^ to replace try.c

$(OBJDIR)/try.o:../file2/try.c \
../file2/inc/try.h
$(CC) -c ../file2/try.c -o $@ -I$(INCDIR) $(C_COMPILE_CONTROL);ls



clean:
rm -rf $(OBJDIR)/*.o


But after I make -f test.mak,*.o are never deleted.that is ,rm -rf $(OBJDIR)/*.o does not work.
So my question is :why?
Thanks.
 
Old 03-28-2002, 12:18 PM   #2
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 31
that's a mighty strange makefile you have here partner.

did you try 'make -f test.mak clean'

default dependency is the first so 'make -f test.mak' is the same as 'make -f test.mak all'

If you want to clean up then build 'make -f test.mak clean all'

I would remove the -r. don't use 'rm -r' unless necessary.

Indent you commands after the target with tabs. I believe you must use tabs and not spaces

eg.
all:$(EXEFILE)
$(EXEFILE):$(OBJDIR)/test.o \
$(OBJDIR)/try.o


For simplicity to can add a rebuild target..

rebuild: clean all

You can change your CC macro to...
CC='gcc -g -c'
and thus clean up your makefile a little

Why are you header files dependencies? You usually don't have to do that unless the make process creates/deletes them.
 
Old 03-30-2002, 03:35 AM   #3
whepin
Member
 
Registered: Dec 2001
Location: thunder bay, on, Canada
Distribution: Windows vista & Ubuntu
Posts: 113

Original Poster
Rep: Reputation: 15
Thanks.

make -f test.mak clean works.

But i still have another question:
Why cannot *.o be automaticlly deleted after make -f test.mak?

Thank you very much for advice!
 
Old 03-30-2002, 12:18 PM   #4
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 31
because 'make -f test.mak' defaults to 'make -f test.mak <first target in test.mak>'

ie. make command simply executes the first target it finds, in your case that's the all target. If you want 'make -f test.mak' to clean you would have to have the 'clean' target as the first target. but then your all target would not be executed unless you do a 'make -f test.mak all'

The solution would be to add a new target, maybe 'cleanbuild' or something.

cleanbuild: all clean

cleanbuild specifies that all and clean are dependencies and have to be built first. And have cleanbuild as the first target. I don't know why you'd want this though. One of the best things about make is that it allows you to rebuild your project without having to build *every* intermediate file.

You can save yourself typing the 'make -f test.mak' by renaming test.mak to 'Makefile'. make looks for the Makefile file as the default makefile. then you can do 'make clean all', and that would also accomplish what you want.
 
  


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 to modify makefile.in or makefile.am QiangWan Linux - Newbie 1 05-17-2005 11:07 AM
how to get (makefile -f makefile )output into the textview widget in Pygtk sailu_mvn Programming 3 02-28-2005 03:57 AM
@ in makefile m_a61 Linux - General 2 09-08-2004 07:09 AM
generate Makefile from Makefile.in without calling ./configure ? chris78 Programming 2 05-02-2004 12:23 PM
makefile pessanimahi Programming 6 09-17-2003 02:34 AM

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

All times are GMT -5. The time now is 12:56 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