Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether function is declared with C preprocessor?

Is it possible to tell the C preprocessor to check whether a function (not a macro) is declared? I tried the following, but it doesn't appear to work:

#include <stdio.h>

int main(void)
{
#if defined(printf)
    printf("You support printf!\n");
#else
    puts("Either you don't support printf, or this test doesn't work.");
#endif
    return 0;
}
like image 614
Michael Avatar asked Nov 17 '09 14:11

Michael


People also ask

What does ## mean in C preprocessor?

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.

What is executed by preprocessor in C?

The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control. In many C implementations, it is a separate program invoked by the compiler as the first part of translation.

Which of the following are C preprocessors?

1. Which of the following are C preprocessors? Explanation: None.


2 Answers

No. Preprocessor runs before the C compiler and the C compiler processes function declarations. The preprocessor is only there for text processing.

However, most header files have include guard macros like _STDIO_H_ that you can test for in the preprocessor stage. However, that solution is not portable as the include guard macro names are not standardized.

like image 133
laalto Avatar answered Oct 06 '22 00:10

laalto


If you look at tools like autoconf you will see that they go through many tests to determine what a computer has or doesn't have, to compile properly, then they set the correct #DEFINES.

You may want to look at that model, and that tool if you are on some flavor of unix, as what you want to do isn't going to be possible, as others undoubtedly are pointing out.

like image 23
James Black Avatar answered Oct 06 '22 01:10

James Black