Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

618 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