4

How can I show the compiler's definition of a typedef during compilation of a C program?

I know I can show how a macro is defined using e.g. #pragma message, see How do I show the value of a #define at compile-time?, but I could not find a similar procedure for typedefs.

Being able to dump a typedefs definition during compile time (or run time) could help debugging programs you did not write yourself.

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • 1
    You can do things with typedef that you can't do (at all easily) with macros — pointers to functions are an example; pointers generally are an issue, but you shouldn't be using typedef on pointers willy-nilly anyway. `#pragma message` is not standard — the C standard does not define what it does. There isn't a standard way to find the mappings for typedefs. Your nearest approach might be to view the output from the C preprocessor (often `-E` or `-P` option to the C compiler, or perhaps run `cpp` as a standalone program), but that's not exactly neat or simple (it is very verbose, usually). – Jonathan Leffler Jan 08 '19 at 22:55
  • 4
    There is always grepping for the typedef definition. – John Bollinger Jan 08 '19 at 22:56
  • 1
    @JohnBollinger Yes `grep` is a quite powerful, and I have used it. But sometimes it seems not enough. For example if the `typedef` is nested in an `#if` - `#elif` clause that depends on other defined symbols. However, in this case, I think using the approach suggested by @JonathanLeffler (in the comment above) and running `gcc -E ... | grep MY_TYPE_NAME` might come to rescue? – Håkon Hægland Jan 08 '19 at 23:18
  • 1
    (a) Easy but requires manual work for each case: Pass an object of the type to a function taking some different type and read the compiler error message. (b) Hard, implementation-dependent, but can be automated: Interpret the debug information produced by the compiler. This requires digging up documentation on the data the compiler generates for use by debuggers and writing a lot of code to interpret that data. Alternatively, some scripting to run a debugger and make it print the type might work. – Eric Postpischil Jan 09 '19 at 03:50

0 Answers0