Alternative to #ifdef ?

Posted on 2000-03-12
Medium Priority
Last Modified: 2009-12-16
I work on a program which has a lot of files made by other programmers. Thi program is used for few different types of Siemens terminals and that's why
someone thought to use #ifdef statements inside the sources and now the sources looks very ugly and hard to be followed. Probably these statements were overused. My question is if the statemets can be reordered in blocks or something in order to make the code easier for debugging? Is there another alternative ?
Question by:jorj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

ufolk123 earned 600 total points
ID: 2611353
Hi jori,
This is the common way for writing a multi-platform code.It is not always possible to group the code at macro level as it will create more redundancy.

e.g if a code has following lines
function f()
/*here comes the system call which has diff sementics on diff systems
#ifdef _UNIX
 Code ( Unix specific)
 other platform

Now one option is to write the function f as
#ifdef _UNIX

code for Unix


code for other platforms


This way code is more readable but the redundancy is very high.
That is main reason of putting only the statement or code chunk level macro usage.

There is one more option if repetative group of statements are encountered in the code.

#define UNIX 0
#define WINDOWS 1

#define CHANGE_SOCK(x,other param)
     case UNIX: ioctl(other param)
     case WINDOWS : ioctlsocket(other param)

These #defines can be in one .h file and you can use it to separate the platform dependent coding.But this is useful only
1) Difference in code is basically system call or logically grouped fixed code.
2) coding style is same i.e code flow is overall same for every platform.

Please get back for more clarifcation if required.



Expert Comment

ID: 2611392
I dont really understand what you question is regarding the reordering,  but, if you would like to remove the #ifdefs, you could put your code under a source control system, and tag different version of it (one tag to see one version of the code, another one to see another version, etc..). I am only familiar with Clearcase as a source control system, and with Clearcase, you can set a view to see a certain version of the source. You could have your common code under the main branch (ie all view would see that code), and have the   code that is different in a branch, that way you dont have to maintain too many different version of the same code.

It you are stuck with all the #ifdefs, and you want to make sure that the defines are ok when u compile one version, you can use the #error prep directive, that will display an error and stop the compilation if you dont have the right defines.

If your program is not that critical to be real fast, you could rearrange the #ifdefs into conditional statements.
LVL 31

Expert Comment

ID: 2613214
If C++ is an option for you you could use namespaces...

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

752 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