Check whether function is declared with C preprocessor?

后端 未结 5 1359
醉梦人生
醉梦人生 2020-12-20 12:31

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:

相关标签:
5条回答
  • 2020-12-20 12:39

    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.

    0 讨论(0)
  • 2020-12-20 12:40

    The preprocessor is a simple program and knows next to nothing about the underlying language. It cannot tell if a function has been declared. Even if it could, the function may be defined in another library and the symbol is resolved during linking, so the preprocessor could not help in that regard.

    0 讨论(0)
  • 2020-12-20 12:52

    Since the preprocessor is not aware of the language C/C++ (it really only does text-replacement) I would guess that this is not possible. Why do you want to do this? Maybe there is another way.

    0 讨论(0)
  • 2020-12-20 12:56

    Strictly speaking no, the preprocessor can't do it on its own. However, you can give it a little help by creating appropriate #defines automatically.

    Normally as was mentioned above, you'd use autotools if on a unix type system. However, you can also create the same effect using a makefile. I recently had cause to detect the "posix_fallocate" function being defined in headers, because I was using uClibc which seemed to omit it in earlier versions. This works in gnu make, but you can probably get a similar thing to work in other versions:

    NOFALLOC := $(shell echo "\#include <fcntl.h>\nint main() { posix_fallocate(0,0,0);}" | $(CC) -o /dev/null -Werror -xc - >/dev/null 2>/dev/null && echo 0 || echo 1)
    ifeq "$(NOFALLOC)" "1"
        DFLAGS += -DNO_POSIX_FALLOCATE
    endif
    
    0 讨论(0)
  • 2020-12-20 12:59

    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.

    0 讨论(0)
提交回复
热议问题