Solved

About ifdef

Posted on 2004-04-19
26
621 Views
Last Modified: 2012-05-04
i need to call a particular function only if a particular file is defined.

i am trying to do this with ifdef. But it is not working properly.
Can anyone give a good example of using a ifdef
0
Comment
Question by:aishwaria
  • 12
  • 10
  • 2
  • +2
26 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
in the other file, define a unique name, like
    #define abcdxyz

Before your function call, use
    #ifdef abcdxyz
        functioncall()
    #endif

Hope this helps.
0
 

Author Comment

by:aishwaria
Comment Utility
i used like this only, but the problem is
one file is in 'a' directory,
where #define abcdxyz
is defined only if the .cpp is included in the makefile
otherwise it is not included.

the condition
#ifdef abcdxyz
        functioncall()
    #endif
is checked in the file which is present in the 'b' directory.

So if i do like this it seems that the #ifdef abcdxyz condition always seems to be false.

0
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
Not used makefiles too often, but would it be ok for you if the #define macro is defined in the makefile and not in the cpp file.

I mean you can do the check of the inclusion of the .cpp file in makefile and then pass the macro as part of the
compiler arguments (-D if i remember correctly).

But then I am not a makefile expert and may be your requirements are different. Just giving ideas to ponder.

Cheers.
0
 

Author Comment

by:aishwaria
Comment Utility
Can u give an example of the above said method.
Because only now i am learing to write makefile.
0
 
LVL 7

Expert Comment

by:leflon
Comment Utility
Hi aishwaria,
do you want to make this decision at compile-time or runtime?

leflon
0
 

Author Comment

by:aishwaria
Comment Utility
both at compile time and runtime
0
 

Author Comment

by:aishwaria
Comment Utility
At compile time i need to compile certain files based on certain conditions.
At runtime i need to call to call only specific functions
0
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
Aishwaria,

When you are using pre-processor defintions such as #define,#ifdef etc the decisions are taken at compile time.

Some things which are not clear are..........

Is your #define statement in a .cpp file (present in dir 'a') OR Is it in a .h file which is included in the .cpp file.

If it is in the .cpp file(dir a) then even if you include it in your makefile for building purposes might not serve the purpose.
This is because the .cpp file(dir b) where you are using #ifdef would not be able to see the #define in a .cpp file unless and
until the .cpp(dir a) file itself is included(#include) in the .cpp file (dir b).

Am I interpreting your problem correctly ? Because I think if #define was in a header file and u included the header file
there is no reason the .cpp file(dir b) should not see it.

If I am wrong then I think we can look for further solutions.

Please let us know.
Cheers.

0
 

Author Comment

by:aishwaria
Comment Utility
My #define is a .c file.

though i had included the .h file of 'a' dir is included in the 'b' dir ,
the condition always fails.

Please give give me a solution i am in urgent need of this solution.

0
 

Author Comment

by:aishwaria
Comment Utility
anupvijay,

As you had said since my #define is a .c file, i think it is not working.

Do u have any solution to solve this problem.
0
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
Aishwaria,

Is it true that you include the .c file in makefile according to some condition ?
I mean something like

if(true)
 makerule 1
else
 makerule2

Or you can post the contents of makefile if possible which deals with this issue.
Cheers.
0
 

Author Comment

by:aishwaria
Comment Utility
ifneq ($(x),$(y))
EXTRA_DIST = x.c y.c
else
EXTRA_DIST = x.c

because otherwise, the functions inside that file y.c is not needed.
0
 
LVL 7

Expert Comment

by:leflon
Comment Utility
aishwaria

@compile-time:
you can use a #define to alter the behaviour of a function doing something like

[xx.h] //some .h file included in your project
#define XXX
void foo();

[xx.c]
#include "xx.h"
...
#ifdef XXX
void foo()       // foo function 1
{
  // do something in here
}
#else
void foo()      // foo function 2, just an empyt function body, thus doing nothing
{
}
#endif

so if you define the XXX the foo() function 1 will be compiled in your project. if you delete the #define in the header file (and making XXX undefined this way) the second foo() function will be called

in some other c file you can use foo()
[zz.c]
#include "xx.h"  //for foo definition
...
void someotherfunc()
{
...
  foo();  // call the foo function
}

@runtime:
if you like to execute a function only if a certain file is present (if this what your initial post means), you have to implement this in your code.
just some pseduo-code:
...
if (file present)                         // using the _stat function or checking the return value of fopen() func
  call function foo()
else
  do nothing or call some other function
endif
...

i don't i the runtime stuff is what you are really looking for, so i kept it a bit short, if you need more info just ask

hth
leflon
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
Assuming that the compiler is GCC there is a compiler preprocessor option "-Dmacro" as per the man page pointed below.

http://hegel.ittc.ukans.edu/topics/linux/man-pages/man1/gcc.1.html#sect7


Instead of defining the #define abcdxyz in any .c file or any .h file you could include the -Dabcdxyz as the compiler
option in your makefile.

So it should look something like this .....

ifneq ($(x),$(y))
EXTRA_DIST = x.c y.c
CFLAGS = -Dabcdxyz  {plus other default options that you have already}
else
EXTRA_DIST = x.c
#might not need anything .....just use the default options.which means no macro is defined.


Now all the #ifdef statements in your other C/CPP  files will not change. The compiler now has a command line
preprocessor argument and will apply it to all the files it compiles....

Try this out and tell is if it works for you.

Cheers & all the best
Anup
0
 

Author Comment

by:aishwaria
Comment Utility
I gave like this,

CFLAGS = -g -O2 -I/usr/include/glib-1.2 -I/usr/lib/glib/include -D_REENTRANT -D_THREAD_SAFE -Wall -DLINUX -DSCTP_MIB_ENABLE

In the b dir, i defined like this
#ifdef SCTP_MIB_ENABLE
func1();
#endif

But this is not working.
have i committed any mistake.
0
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
It looks correct. That is strange if it is not working. A little bit effort with the -E,-dM and -dD options are required to
find out what is going wrong. Een changing the order of the -D defitions is not a bad idea. Although the man page
points out that the order doesn't matter. But you never know.

-E,-dM and -dD options will help you to find out what the pre processor is doing just before it goes for compilations.

Also make sure the compilation command for .c file in the makefile includes this CFLAGS....ok?

Try and let know
Cheers.
0
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
BTW,what is the compiler and the version that you are using. Never mentioned it.
0
 

Author Comment

by:aishwaria
Comment Utility
gcc compiler
0
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
Try

#if SCTP_MIB_ENABLE    /*changed #ifdef to #if */
func1();
#endif

instead of

#ifdef SCTP_MIB_ENABLE
func1();
#endif
0
 

Author Comment

by:aishwaria
Comment Utility
No it is not working

0
 

Author Comment

by:aishwaria
Comment Utility
Should i must redefine the SCTP_MIB_ENABLE macro in the .C file.

0
 
LVL 2

Accepted Solution

by:
anupvijay earned 35 total points
Comment Utility
What I understand from the GCC spec is that -Dxyz will assign xyz a value of 1.
That is why I had changed the check to #if.


While you trying out,  try -Dxyz=0 and -Dxyz=1 in the makefile rules and then do

#if (xyz=1)
....
#else
....
#endif

But it is really strange that the above solutions are not working for you.
I am really sorry but I do not have a unix machine now where I can actually try things out and then tell you.
That is why I am asking you to try it out.

Cheers.
0
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility
No Aishwaria, a redefinition is not needed in any source file. -D is just for that.

Are these options in effect for you ?
-D_REENTRANT -D_THREAD_SAFE -DLINUX


Cheers.
0
 

Author Comment

by:aishwaria
Comment Utility
Thanks for your help.
I had committed a blunder.
i forgot to undo a change which i made it earlier.
After removing it is working fine.
I had learnt a new method because of you.
Thanks a lot.

0
 
LVL 2

Expert Comment

by:anupvijay
Comment Utility

What was the problem anyway ? I mean why was the mistake you made, affecting the macro ?
Just curious.

Congratulations anyway, that you have done it.
Cheers,
Anup
0
 
LVL 9

Expert Comment

by:Cayce
Comment Utility
Easy, just to like in the assert() macro

#undef assert
#if defined NDEBUG
#define assert(test) (void)0
#else
#define assert(test) _assert(test_expression, etc, etc, etc);
#endif
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now