1

I know "gcc -E main.c" option gives all the values pre-processed output. Is there a way to just expand the user defined macros?

For example

#define MACRO(z) z+z

c = a + (MACRO(z))

When I use this hypothetical gcc option, I should see:

c = a + (z+z)

I do not want any other system-defined MACRO's to be expanded. Is there an option in GCC?

4
  • 1
    What constitutes a user-defined macro? Is it just the ones written in this source file? Or does it include macros in user-defined headers (as opposed to system-defined headers)? One possibility might be to map #include to @include (for sake of argument), then use gcc -E, if necessary, remapping the @include back to #include. I'm not sure there'd be many other ways to do it. You could be selective and only map #include <angel-brackets.h> leaving #include "double-quotes.h" alone, so that the macros in those are expanded (amongst other side effects). Commented May 21, 2015 at 23:38
  • You might also look at the technology in Coan. Commented May 21, 2015 at 23:41
  • @Jonathan: Not just source file macros ,the one in user-defined header file too. Say for example you are writing a file that has macros and you want to see how the macro-usage has taken form after pre-processing. I do not need the info right now, because I was trying an simpler example and got it resolved already. But I remember using a flag with g++ in college, which would spit out the pre-processed code with just user-defined macros. It was really use ful when we were using multi-line macros to simulate 'Templates (c++)'. Thought would be an easier find in the internet. But no luck. :( Commented May 22, 2015 at 0:19
  • I was afraid of that; it greatly complicates the processing (because to do the @include trick, you have to map copies of the files that are themselves included via #include "header.h". Which is the sort of reason it hasn't been done as standard. I'm not sure which flag you might have been using in college -- it doesn't sound like a standard one, or you'd be able to find it by going to the GCC documentation, reading the relevant CPP manual (e.g. CPP 5.1.0 "Invocation"). Commented May 22, 2015 at 0:40

2 Answers 2

1

Effectively what you want to do is to have the preprocessor ignore errors opening include files. I think there is a compiler with such a feature, but GCC isn't it. (Or maybe I'm confusing it with the GCC feature to ignore missing include files when generating dependency files.)

However what you can do instead is to create a shadow directory of empty files matching all the names and sub-directories for the system headers. You can then use the -nostdinc (to ignore the system include directories) and -I option (to point at your new shadow directory).

The shadow directory can be created with something like:

mkdir $HOME/tmp/emptystdinc
cd $HOME/tmp/emptystdinc
find /usr/include -type d -print | sed 's|/usr/include|.|' | xargs mkdir -p
find /usr/include -type f -print | sed 's|/usr/include|.|' | xargs touch

Now see what your post-processed code looks like:

cc -E -nostdinc -I$HOME/tmp/emptystdinc test.c

Good luck! This works fine for me!

Sign up to request clarification or add additional context in comments.

Comments

0

Quoting from cpp man page:

-undef Do not predefine any system-specific or GCC-specific macros. The standard predefined macros remain defined.

This means that macros such as __FILE__ or __LINE__ will be expanded, but this may be sufficient for your needs. You can also undefine these standard predefined macros using -U. The full list can be found here.

2 Comments

This might be part of the solution, but you probably have to do something about the macros that are included from #include <stdio.h> etc.
Indeed. Since the OP did not use an #include in his question, I was not really sure. Many people abuse the C preprocessor for other means, and in this case they do not use the system headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.