Solved

Makefile use of ifdef

Posted on 2012-04-03
5
1,160 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 32

Assisted Solution

by:sarabande
sarabande earned 100 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now