[LinuxFocus-icon]
Castellano Chinese Deutsch English Franšais Nederlands

This document is available in: English  Francais  

[Photo of the Author]
by Guido Socher (homepage)

About the author:

Guido really likes it when a computer is tailored to his needs and looks like he wants it. That's why he is using Linux.
Content:


 

LF Tip: #defines for writing portable C-code

idea

Abstract:

This is a small tip. From now on LinuxFocus will have at least one new tip every month. If you have some ideas for a new tip then send them to guido("at" sign)linuxfocus.org

_________________ _________________ _________________

 

Introduction

Java is known as "write once works never" and C is "write once, compile and works always". In other words C code is very portable and the beauty is also that you can have preprocessor #ifdef statements to distinguish between the different platforms. This way you have just one piece of code which can be recompiled on different systems even without running an auto configuration script before compilation.

Here is an example:
#ifdef sun
	// solaris
static char *port="/dev/ttya";
#endif

#ifdef linux
	// linux
static char *port="/dev/ttyS0";
#endif
 

What #define is available?

The question is only how do you know that there is a #define linux or __linux__? ... and what other #defines do we have?

It's quite simple. Just run this command and you get a list of pre-defined #define statements:
 touch /tmp/foo.h ; cpp -dM /tmp/foo.h
That's all.  

Talkback form for this article

Every article has its own talkback page. On this page you can submit a comment or look at comments from other readers:




Webpages maintained by the LinuxFocus Editor team
© Guido Socher
"some rights reserved" see linuxfocus.org/license/
http://www.LinuxFocus.org
Translation information:
en --> -- : Guido Socher (homepage)

2005-12-21, generated by lfparser version 2.53