Does #ifdef (or other Preprocessor Directives) Work for Function Declarations (to Test for Function Existence)?

10,994

Declarations should be available at pre-processing, so it should work, shouldn’t it?

The pre-processor operates before the compilation (hence the "pre") so there are no compiled symbols at that point, just text and text expansion. The pre-procesor and the compiler are distinctly separate and work independantly of each other, except for the fact that the pre-processor modifies the source that is passed to the compiler.

The typical pattern to doing something like with the pre-processor is to pair the function declaration with the function usage using the same define constant:

#define FOO

#ifdef FOO
 void foo(int);
#endif

#ifdef FOO
   printf("foo exists");
#endif
Share:
10,994
Synetech
Author by

Synetech

“Hobby programmer”, for over 27 years. A few of my favorites languages include assembler ❤️, C++, Pascal, and PHP, but my current go-tos (pun intended) these days tend to be AutoHotkey and JavaScript. I used to be a bit of a compile-snob, but I find scripting languages to be more convenient now. Secondary languages I use include anything and everything (I keep trying to pick up new ones just because, including those crazy esoteric ones, but especially anything that is remotely useful). I mostly do library and utility programming, creating either libraries of functions for use in other programs, or small, specific tools to do something that needs to be done but for which no existing programs already exist. (I can’t count the number of times that I’ve needed a program to do something, and finding no existing tools, had to resort to writing one myself.) I’ve been mired in SE pretty much from its beginnings. Unfortunately I became soured towards the SE network a few years ago because of a user on SU and the mods’ inaction against his behavior, so I have not really been active on the sites since, however I do occasionally follow up when I get a notification of a response to something I have previously posted.

Updated on June 04, 2022

Comments

  • Synetech
    Synetech almost 2 years

    Why doesn’t the following code work as expected?

    void foobar(int);
    
    #ifndef foobar
      printf("foobar exists");
    #endif
    

    It always prints the message; it obviously cannot detect the existence of a function as an entity. (Is it an over-loading issue?)

    Why can’t #ifdef (or its variants) detect function declarations? Declarations should be available at pre-processing, so it should work, shouldn’t it? If not, is there an alternative or work-around?

  • Synetech
    Synetech almost 13 years
    I know the preprocessor works before compilation, but the function declarations are already present in the text at that point (in this case, two lines before). I suppose the problem is that the preprocessor doesn’t understand declarations; only pre-proc directives. So then there is no practical/automatic way to test for function existence? (Manually creating a pre-proc define for each function is too much work.)
  • dkackman
    dkackman almost 13 years
    Yup. The pre-processor doesn't understand C or C++ at all, so a function declration is nothing more than a bit of text. What particular problem are you trying to solve? Perhaps a function pointer or something else is what you are really looking for. This works when, at runtime, you want to see if a dll exports a particular method and use it if it does. But if you are truly looking to do something like this during compilation the above is how it's done.
  • Synetech
    Synetech almost 13 years
    I guess I got spoiled a bit when I did some work in JavaScript and you could do things like easily check if a function/property exists.