Suppose there is such a code:

typedef int i32; 

How to find out if i32 type i32 already been announced? That is, something like #ifdef for typedef only.

  • The preprocessor will definitely not be able for typedef. But if you declare a type through define, you can. - KoVadim
  • And why do you need it? This case is not a problem XY? - VTT
  • @VTT, well, actually, the question is theoretical. - eanmos
  • Well, in theory, redeclaration of an alias of type became available only starting from C11. And so you will receive the warning -Wtypedef-redefinition . - VTT

3 answers 3

Do not know.

If the far-sighted author of the code foresees the need for such verification in the future, he usually accompanies the definitions of types with the definition of additional macro. For example, the standard header file <stdbool.h> contains the macro definition __bool_true_false_are_defined with a value of 1 .

    There is a double compilation method, I know how to do this in Linux through a Makefile . First a simple file is compiled with typedef . The compilation error code is then checked, and #define TYPEREDEFINIED .

     Makefile : main.o : main.c main.h ..TAB..: if gcc checktypedef.c &> /dev/null ; then ; else OPTIONFORGCC = -DTYPEREDEFINIED ; fi ..TAB..: gcc OPTIONFORGCC main.c .. checktypedef.c : # include <все заголовки из main> typedef unsigned char BYTE ; main.c : # include <все заголовки> # ifdef TYPEREDEFINIED # error Опять! # endif 
    • There is an assumption that there will be no error code, even with -pedantic, -std = need to be announced and -Wtypedef-redefinition - NewView

    It is possible to declare all used header in one test.c file test.c for example, then:

     gcc -E test.c | grep "int32my" 

    and see the result :)

    • ? This is not a legal way - Andrio Skur
    • What is the 'legal' way? to understand what is 'not legal'? :) If you are interested in the result, then it works. - NewView
    • The legal way is that which is given by the authors in languages ​​both official and working. Your example code requires calling the command in compile-time, which is not so easy. In addition, for example, it will work on the type "__int32my_internal". - Andrio Skur
    • That is, this method works as horrible - Andrio Skur