Solved

About ifdef

Posted on 2004-04-19
26
622 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
ID: 10858083
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
ID: 10858106
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
ID: 10858133
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
ID: 10858254
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
ID: 10858277
Hi aishwaria,
do you want to make this decision at compile-time or runtime?

leflon
0
 

Author Comment

by:aishwaria
ID: 10858289
both at compile time and runtime
0
 

Author Comment

by:aishwaria
ID: 10858374
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
ID: 10858379
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
ID: 10858413
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
ID: 10858524
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
ID: 10858615
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
ID: 10858648
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
ID: 10858694
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
Will my email signature work in Office 365?

You've built an email signature using raw HTML code in Office 365, but you can't review how it looks with Transport Rules. So you have to test it over and over again before it can be used. Isn't this a bit of a waste of your time? Wouldn't a WYSIWYG editor make it a lot easier?

 
LVL 2

Expert Comment

by:anupvijay
ID: 10858722
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
ID: 10858997
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
ID: 10859226
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
ID: 10859336
BTW,what is the compiler and the version that you are using. Never mentioned it.
0
 

Author Comment

by:aishwaria
ID: 10859359
gcc compiler
0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10859396
Try

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

instead of

#ifdef SCTP_MIB_ENABLE
func1();
#endif
0
 

Author Comment

by:aishwaria
ID: 10859420
No it is not working

0
 

Author Comment

by:aishwaria
ID: 10859499
Should i must redefine the SCTP_MIB_ENABLE macro in the .C file.

0
 
LVL 2

Accepted Solution

by:
anupvijay earned 35 total points
ID: 10859501
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
ID: 10859520
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
ID: 10859611
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
ID: 10859682

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
ID: 10872634
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 137
Test against App 49 133
Handling string inputs in C/Linux 23 179
An API detour question 7 79
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now