[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Unix Makefile

Posted on 2004-08-12
20
Medium Priority
?
1,599 Views
Last Modified: 2013-12-26
How to assign value to a variable on given condition in unix Makefile. for eg
if a>10
then a =1
else
a = a+1

thanx
0
Comment
Question by:princyrajan
  • 7
  • 6
  • 4
  • +3
20 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11781872
0
 
LVL 11

Expert Comment

by:avizit
ID: 11782185
Though it seems you can have only the following four conditons which can be checked

1) ifeq
2) ifneq
3) ifdef and
4) ifndef

so you cannot do a check like a>10  

/abhijit/
0
 
LVL 6

Expert Comment

by:Talmash
ID: 11783293
if a>10
then a =1
else
a = a+1

use :

if ($a > 10) then
       set a = 1
else
      @ a+=1
endif

1) please take care of the spaces as exactly as I wrote .
2) at the head of the file write :
   #! /bin/csh -f
3) chmod 755 file (to enable execution)

tal
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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 14

Assisted Solution

by:chris_calabrese
chris_calabrese earned 200 total points
ID: 11784317
I'm pretty sure talmash is pulling your leg here. But this really isn't the sort of thing that Make is good at, or even intended for.

It _is_ possible to set _static_ variables at the beginning of a makefile, and you can reference them from inside the shell-code action-bits like this:
  a="fubar"
  b="bozo"

  b: $(a)
        if [ -d "$(a)" ]; then mkdir "$(b)"; else touch "$(b)"; fi

But if you need anything more complex, then your options are either to not use Make at all and use a general-purpose scripting language (Perl and shell being the obvious choices), or to use a script to generate a makefile.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11788707
Can you give us some details on why you need to do this.  There may well be a better way of achieving what you want.
0
 

Author Comment

by:princyrajan
ID: 11790649
I am having a Makefile in which the library path have to be set (after checking
condition) according to the machine for eg: AIX or Solaris

thanx
0
 
LVL 11

Expert Comment

by:avizit
ID: 11790666
In that case  I think you can use the

"ifeq" or "ifneq" conditionals ..

0
 

Author Comment

by:princyrajan
ID: 11790935
VERSION      =
ifeq ($(VERSION),DEBUG)
VERSION =-g
endif
------------------
but, in this case if I assign a value for VERSION in command line (while make -ing a file) as DEBUG, the if condition's true part doesn't get executes. why?

thanx
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 800 total points
ID: 11791557
I tried the following example ,seems to work for me

hello.c
-------------
#include <stdio.h>

int main(){
     printf("hello\n");
     return 0;
}

Makefile
-------------
CC=gcc

ifeq ($(TEST),gee)
        CFLAGS=-g
else
        CFLAGS=-pg
endif

hello:hello.c
        $(CC) $(CFLAGS) -o hello hello.c
=================================


now i can give the following two make commands and the results are shown too,

$ make TEST=gee
gcc -g -o hello hello.c

$ make TEST=nogee
gcc -pg -o hello hello.c

guess thats what you wanted.

btw maybe in yuor example you can just pass the version thing on the command line and you need not have to use conditional for the makefile

as in

make VERSION=-g


/abhijit/
0
 

Author Comment

by:princyrajan
ID: 11792391
Hi,
I used, (AIX is my machine)

OSNAME=`uname`
ifeq ($(OSNAME),AIX)
..
..
endif

in my makefile, but the true part doesn't get executes.

Thanx
0
 
LVL 11

Expert Comment

by:avizit
ID: 11792444
i am not sure  if OSNAME=`uname`  is allowed within a makefile

but what you want can be achieved by


removing the line  OSNAME=`uname`  from the makefile
and putting the following in the command like

make OSNAME=`uname`


0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1000 total points
ID: 11796539
OSNAME=`uname -s`
AIX-PATH=path/AIX
SunOS-PATH=path/Solaris
VERSION=
g-VERSION=-g
here-we-go:
        @echo path=$($(OSNAME)-PATH)
        @echo opt=$($(DEBUG)-VERSION)
# end-of-Makefile

# don't forget the tabs in the target, then it can be improved inmany ways
# you can test with:
  make
  env DEBUG=g make
0
 

Author Comment

by:princyrajan
ID: 11808718
No my friend its not working for me. Give me some other eg:

Thanx
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11808803
which part does not work?
if unshure try using -d and/or -dd option
0
 

Author Comment

by:princyrajan
ID: 11817541
"echo opt" is getting printed but not "echo path" (OSNAME).

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11818337
please post result verbatim (drag&drop)
but I assume that uname -s returns something different
please adapt the "AIX"-PATH part in
  AIX-PATH=path/AIX
to what uname -s returns (must be exact string literal, case-sensitiv!)
0
 

Author Comment

by:princyrajan
ID: 11818593
In my Machine "uname -s" returns AIX.
Generally 'uname' gets executed and its value get stored in variable in shell script but it is not working in Makefiles.

If I execute ur code it giives o/p as follows
o/p:
path=
opt=-g


-Thanks a lot
0
 
LVL 11

Expert Comment

by:avizit
ID: 11818703
can you just invoke make as

 make OSNAME=`uname`

0
 

Author Comment

by:princyrajan
ID: 11818902
In my project I am just going to include this makefile. So its not possible to invoke as "make OSNAME =`uname`

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11818939
is this AIX's bundled make ?
then please check man make or other docs how to use shell inside makefile (sorry, don't have AIX handy)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

591 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