Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Back: Macro management
Forward: Looping
 
FastBack: Looping
Up: Features of M4
FastForward: Writing macros within the GNU Autotools framework
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

21.4.3 Conditionals

Macros which can expand to different strings based on runtime tests are extremely useful--they are used extensively throughout macros in GNU Autotools and third party macros. The macro that we will examine closely is ifelse. This macro compares two strings and expands to a different string based on the result of the comparison. The first form of ifelse is akin to the if/then/else construct in other programming languages:

 
ifelse(string1, string2, equal, not-equal)

The other form is unusual to a beginner because it actually resembles a case statement from other programming languages:

 
ifelse(string1, string2, equala, string3, string4, equalb, default)

If `string1' and `string2' are equal, this macro expands to `equala'. If they are not equal, m4 will shift the argument list three positions to the left and try again:

 
ifelse(string3, string4, equalb, default)

If `string3' and `string4' are equal, this macro expands to `equalb'. If they are not equal, it expands to `default'. The number of cases that may be in the argument list is unbounded.

As it has been mentioned in 21.3.2 Macros and macro expansion, macros that accept arguments may access their arguments through specially named macros like `$1'. If a macro has been defined, no checking of argument counts is performed before it is expanded and the macro may examine the number of arguments given through the `$#' macro. This has a useful result: you may invoke a macro with too few (or too many) arguments and the macro will still be expanded. In the example below, `$2' will expand to the empty string.

 
define([foo], [$1 and $2])dnl
foo([a])
=>a and

This is useful because m4 will expand the macro and give the macro the opportunity to test each argument for the empty string. In effect, we have the equivalent of default arguments from other programming languages. The macro can use ifelse to provide a default value if, say, `$2' is the empty string. You will notice in much of the documentation for existing Autoconf macros that arguments may be left blank to accept the default value. This is an important idiom that you should practice in your own macros.

In this example, we wish to accept the default shell code fragment for the case where `/etc/passwd' is found in the build system's file system, but output `Big trouble!' if it is not.

 
AC_CHECK_FILE([/etc/passwd], [], [echo "Big trouble!"])


This document was generated by Gary V. Vaughan on February, 8 2006 using texi2html

 
 
  Published under the terms of the Open Publication License Design by Interspire