Solved

Makefile use of ifdef

Posted on 2012-04-03
5
1,188 Views
Last Modified: 2012-04-11
Is it possible for a makefile to be aware of what is defined in the source code?  For example, in a header file I have:
#define T5

I'd like the makefile to recognize the definition and build differently based on that:
ifdef T5
CH_Manager: $(OBJ)
      $(CC) $(CCOPT) $(CFLAGS) -o $(OUTDIR)CH_Manager_T5 $^
endif

This doesn't work.  
I'd like to change the name of the resulting executable based on what's defined. Is it possible?  If not is there another way to do it?

Thanks.
0
Comment
Question by:JohnSantaFe
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 37803889
You can use 'ifdef' in your makefile to set a definition for the source code, that to me seems the better way round:

ifdef T5
CH_Manager: $(OBJ)
      $(CC) $(CCOPT) $(CFLAGS) -DT5 -o $(OUTDIR)CH_Manager_T5 $^
endif
0
 

Author Comment

by:JohnSantaFe
ID: 37803964
Yea, I understand that.  The disadvantage is you don't get the highlighting in the IDE (Eclipse) that shows you what is being defined and what is not.  I find that very useful.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 300 total points
ID: 37804818
The traditional (unix) approach to this, is to have a configure script that is run before building the source code, and serves to customize the build for the specific platform and/or user preferences.

See eg. autoconf (http://www.gnu.org/software/autoconf/) for a tool that facilitates the generation of such configure scripts.

The configure script would generate a config.h file, which your IDE can read, and provide the highlighting you desire.
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 100 total points
ID: 37805326
you could make a new target in the makefile

T5:  $(EXE)
      sometool  $(EXE) -T5

Open in new window


it is assumed that $(EXE) is the target which makes the executable. sometool would check whether the executable has the option defined - for example by reading the configuration or simply by starting the executable with a special option - and if so would copy or rename the executable.

Sara
0
 

Author Comment

by:JohnSantaFe
ID: 37834420
I appreciate the ideas.  It looks like the 'right' way to do this is with autoconf, I was hoping for something a little simpler.
Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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