?
Solved

makefile and conditional compilation

Posted on 2002-05-23
14
Medium Priority
?
980 Views
Last Modified: 2008-03-06
I have an application with *many* source files that can be compiled "normally" or with a "debug" option.
The compilation is dependant on the -DDEBUG compiler switch.

How can I set up a makefile so that I can type in either:

make app_debug (to make the debug version)
or
make app (to make the non-debug version)

Basically the problem is this:
Once I make the debug version (using the -DDEBUG switch) the object files are timestamped as being younger than the source files. So when I try to make the non-debug version nothing gets "re-compiled".

Currently I link the source files to different names which causes differnet object file names to be created.

i.e.
I link source.c to source_d.c
so that
source.c compiles to source.o
source_d.c compiles to source_d.o

I think this is a clumsy way to do waht I need, can anyone suggest a more elegant approach?

Paul
0
Comment
Question by:zebada
  • 9
  • 4
14 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7030905
You could just specify a different output path for each target like e.g. VC++ does - this would keep the (equally named) object files n different directories, so that no time-stamp confusion would take place...
0
 
LVL 30

Expert Comment

by:Axter
ID: 7030925
You can also change the name of the output targets.
For example, all the debug versions you can name foo.debug_o and release version foo.o
0
 
LVL 6

Author Comment

by:zebada
ID: 7030991
The real question is how do I do that in the makefile?

I am currently using my own implicit .c.o: rule to compile my c source files.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 30

Expert Comment

by:Axter
ID: 7031024
Is this for Unix/Linux or Dos?
0
 
LVL 30

Expert Comment

by:Axter
ID: 7031027
If you're doing Unix/Linux, you can create two script files.

One haveing the following:

#*********************************************************
# Set environmental variable
setenv REL_OR_DBUG debug_
# Launch make file with commandlines given to script file
make NameOfMyMakeFile %1 %2 %3 %4 %5
#*********************************************************

The other script file can be the following:
#*********************************************************
# Set environmental variable
setenv REL_OR_DBUG ""
# Launch make file with commandlines given to script file
make NameOfMyMakeFile %1 %2 %3 %4 %5
#*********************************************************


Then in your make file have the following:
PRGSUB = $(CSRCS:cpp=$(REL_OR_DBUG)o)
PRG  = $(PRGSUB:c=$(REL_OR_DBUG)o)
OBJS = $(PRG)
0
 
LVL 30

Expert Comment

by:Axter
ID: 7031035
You can also just do one script file and make the first command line argument the environmental variable for REL_OR_DBUG.

Example:
#*********************************************************
# Set environmental variable
setenv REL_OR_DBUG %1
# Launch make file with commandlines given to script file
make %2 %3 %4 %5 %6 %7
#*********************************************************

You would call the above script file in the following manner.

MyScriptFileName debug_ NameOfMyMakeFile

If you have additonal command lines just added to the end.
Example:

MyScriptFileName release_ NameOfMyMakeFile buildme

Or

MyScriptFileName rel_ NameOfMyMakeFile clean

0
 
LVL 30

Expert Comment

by:Axter
ID: 7031045
Correction on the makefile syntax:

PRGSUB = $(CSRCS:cpp=${REL_OR_DBUG}o)
PRG  = $(PRGSUB:c=${REL_OR_DBUG}o)
OBJS = $(PRG)

It may be hard to tell with the fonts on this web site, but the REL_OR_DBUG variable has "curly cue" {} wrapped around them.
Environmental variables get curly cues {}, and NOT the parenthesis "()" that normal make file variables get.
0
 
LVL 6

Author Comment

by:zebada
ID: 7031248
Platforms:
SCO
HP-UX
Solaris
Tru64 (DEC Alpha OSF1)
Linux
AIX

If I have something like this in my makefile:

HDR=            a.h b.h c.h

SRC=            a.c b.c c.c

OBJ=            $(SRC:.c=.o)

prog:           $(OBJ)

$(OBJ):         $(HDR)

This is my .o.c: rule:
.c.o:           $<
                $(CC) $(ALL_CFLAGS) -c $<

How can I make the $(CC) command output to the different object file names (for debug and non-debug) based on the value of an env variable?

0
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 7031781
Here's a section of one of my old make files.
Just ignore what you don't need.
If you're interested, I can also show you how to make different targets, depending on your unix platform, by using `uname`


######################################################################
.SUFFIXES: .c .cpp .h .$(REL_OR_DBUG)o

.cpp.$(REL_OR_DBUG)o:; @echo "$(<:cpp=cpp):1: Creating: Object File from *.cpp" ${XPLATFORMX}/${COMPILER_OBJECT_ID}/$(<:cpp=$(REL_OR_DBUG)o)"..."
     $(CXX) $(CXXFLAGS) -c $< -o ${XPLATFORMX}/${COMPILER_OBJECT_ID}/$(<:cpp=$(REL_OR_DBUG)o)
     cp -fp -- ${XPLATFORMX}/${COMPILER_OBJECT_ID}/$(<:cpp=$(REL_OR_DBUG)o) ${XPLATFORMX}/${COMPILER_OBJECT_ID}/../..

.c.$(REL_OR_DBUG)o:; @echo "$(<:c=c):1: Creating: Object File from *.c" ${XPLATFORMX}/${COMPILER_OBJECT_ID}/$(<:c=$(REL_OR_DBUG)o)"..."
     $(CC) $(CFLAGS) -c $< -o ${XPLATFORMX}/${COMPILER_OBJECT_ID}/$(<:c=$(REL_OR_DBUG)o)    
     cp -fp -- ${XPLATFORMX}/${COMPILER_OBJECT_ID}/$(<:c=$(REL_OR_DBUG)o) ${XPLATFORMX}/${COMPILER_OBJECT_ID}/../..

)
0
 
LVL 30

Expert Comment

by:Axter
ID: 7043261
zebada,
Did the above script code answer your question?
0
 
LVL 6

Author Comment

by:zebada
ID: 7043269
I'll take a look at it later this week - been too busy.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7043274
OK
0
 
LVL 6

Author Comment

by:zebada
ID: 7171575
Thanks Axter,
Sorry it took so long - the project finally got the go-ahead to redesign the make files.
The suffix substitution did the trick :)
0
 
LVL 30

Expert Comment

by:Axter
ID: 7171924
Glad to be of help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question