Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

About ifdef

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
aishwaria
Asked:
aishwaria
  • 12
  • 10
  • 2
  • +2
1 Solution
 
Sjef BosmanGroupware ConsultantCommented:
in the other file, define a unique name, like
    #define abcdxyz

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

Hope this helps.
0
 
aishwariaAuthor Commented:
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
 
anupvijayCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
aishwariaAuthor Commented:
Can u give an example of the above said method.
Because only now i am learing to write makefile.
0
 
leflonCommented:
Hi aishwaria,
do you want to make this decision at compile-time or runtime?

leflon
0
 
aishwariaAuthor Commented:
both at compile time and runtime
0
 
aishwariaAuthor Commented:
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
 
anupvijayCommented:
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
 
aishwariaAuthor Commented:
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
 
aishwariaAuthor Commented:
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
 
anupvijayCommented:
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
 
aishwariaAuthor Commented:
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
 
leflonCommented:
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
 
anupvijayCommented:
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
 
aishwariaAuthor Commented:
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
 
anupvijayCommented:
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
 
anupvijayCommented:
BTW,what is the compiler and the version that you are using. Never mentioned it.
0
 
aishwariaAuthor Commented:
gcc compiler
0
 
anupvijayCommented:
Try

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

instead of

#ifdef SCTP_MIB_ENABLE
func1();
#endif
0
 
aishwariaAuthor Commented:
No it is not working

0
 
aishwariaAuthor Commented:
Should i must redefine the SCTP_MIB_ENABLE macro in the .C file.

0
 
anupvijayCommented:
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
 
anupvijayCommented:
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
 
aishwariaAuthor Commented:
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
 
anupvijayCommented:

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
 
CayceCommented:
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

Technology Partners: 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!

  • 12
  • 10
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now