The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help understanding a program Toxic Shell Programming and Scripting 1 03-19-2006 09:50 PM
A little help understanding FIFOs? deckard Linux 0 11-01-2005 09:46 AM
Understanding traceroutes Bobby IP Networking 2 03-14-2005 01:35 PM
need further understanding of init.d jigarlakhani UNIX for Advanced & Expert Users 1 09-20-2002 12:11 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-12-2007
Registered User
 

Join Date: Apr 2007
Posts: 75
Stumble this Post!
Understanding this Makefile

I have this program which has lots of source files in the directories
Code:
src
src/dir1 
src/dir2
src/dir3... and so on
I am trying to understand the following Makefile:

Code:
CC = gcc
CFLAGS= -g -c -D_REENTRANT
SOURCES = src/main.c src/dir1/a.c src/dir1/b.c src/dir2/x.c src/dir2/y.c ...and so on
OBJS = src/main.o src/dir1/a.o src/dir1/b.o src/dir2/x.o src/dir2/y.o ...and so on
TARGETS = prog.exe

%.o : %.c
        $(CC) $(CFLAGS) -o $@ $?

${TARGETS}:  ${OBJS}
        $(CC) ${OBJS} -o ${TARGETS}
First of all, I am trying to understand what does $@ $? do?? And also what does the wild character % in %.o : %.c stand for ?

And, since I have a lot of source files spread out in multiple directories and lots of corresponding directories.. the macros SOURCES and OBJS are getting quite ugly.. is there any way around this ?

(I couldve also used macros for the directory names, but it is still getting very ugly)
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-12-2007
Registered User
 

Join Date: Apr 2007
Posts: 75
Stumble this Post!
It would be much better if i could say:

Code:
SOURCES = src/*.c    src/dir1/*.c    src/dir2/*.c
OBJS       = src/*.o    src/dir1/*.o    src/dir2/*.o
But that does not work
Reply With Quote
  #3 (permalink)  
Old 06-12-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Stumble this Post!
$@ is the target of the dependancy

$? are the prequisites for each dependancy rule

The %.o: %.c means in order to build a file with this extension from a file of this extension follow this rule.

Personally I think wild cards are bad news in Makefiles. You want to know exactly what you are building.

While we are at it, I also I don't recommend requiring gmake.

Also, personally, I think that objects should be built in a totally different tree to the source.
Reply With Quote
  #4 (permalink)  
Old 06-13-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,794
Stumble this Post!
And I think that persistent objects be kept in source control along with the primary source. Source control is a must.
Reply With Quote
  #5 (permalink)  
Old 06-13-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Stumble this Post!
I was referring to .o files. As in I recommend keeping the compiled objects built in a separate tree to the source especially when you compile for multiple architectures.
Reply With Quote
  #6 (permalink)  
Old 06-13-2007
ilko_partizan's Avatar
Registered User
 

Join Date: Apr 2007
Location: EU, Bulgaria
Posts: 33
Stumble this Post!
This article will help you to understand "make".
An Introduction to the UNIX Make Utility

Best regards,
Iliyan Varshilov

Last edited by ilko_partizan; 06-18-2007 at 05:18 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0