Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check how a macro "expands" in C or Objective-C

A macro is a preprocessor right. Sometimes we set things right sometimes we don't.

Wouldn't it be nice to once in a while expand the macro and see how it works?

like image 688
user4951 Avatar asked Sep 30 '11 06:09

user4951


People also ask

What handles macro expansion in C?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

How do you check if a macro is defined in C?

To check – A Macro is defined or not, we use #ifdef preprocessor directive. To check whether a Macro is defined or not in C language – we use #ifdef preprocessor directive, it is used to check Macros only. If MACRO_NAME is defined, then the compiler will compile //body (a set of statements written within the #ifdef ...

What is a macro Objective C?

Macros can be used in many languages, it's not a specialty of objective-c language. Macros are preprocessor definitions. What this means is that before your code is compiled, the preprocessor scans your code and, amongst other things, substitutes the definition of your macro wherever it sees the name of your macro.

What type is __ LINE __?

__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.


1 Answers

You can do this with most compilers, e.g. with gcc:

$ gcc -E

this just runs the preprocessor and then stops, giving you the pre-processed output on stdout.

If you're using Xcode though you don't even need to do this - depending on what version of Xcode you're using you can just select Preprocess from the Build menu (that's what its called in Xcode 3.x - if you're using Xcode 4 it may have been moved/renamed). See: Xcode Preprocessor Output

like image 104
Paul R Avatar answered Nov 08 '22 02:11

Paul R