Go Premium for a chance to win a PS4. Enter to Win


#define, #include inside a MACRO

Posted on 1998-11-18
Medium Priority
Last Modified: 2013-11-20
Is there a way to put a #define or a #include inside a MACRO ?


#define MY_MACRO(a) \
  #define MY_CONSTANT_##a

#define MY_MACRO2(a) \
  #include #a

I know that these macros can't compile but I would like a way to make such macros.
Question by:jhendrix
  • 3
  • 2

Author Comment

ID: 1324932
Adjusted points to 50
LVL 11

Expert Comment

ID: 1324933
No, you can't.

See Chapter 16 of the C++ FIDS.

The closest you can come is the use of the #include directive with a macro instead of a string name.

#define MYFILE "foo.h"
#include MYFILE

You can generate a macro for the include directive, too:

#define str(s) # s
#define xstr(s) str(s)
#define INCFILE(n) vers ## n
#include xstr(INCFILE(2).h)

B ekiM


Author Comment

ID: 1324934

I guess you've actually answered the question ("No I can't") but I really need a way to do this.
As for generating a macro for the include directive, that doesn't help me at all and is pretty basic macro stuff.

Thanx anyways.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Accepted Solution

payn earned 150 total points
ID: 1324935
As much as you may want to do this, it's not possible in C++. Partly this is because definitions like this lead to ambiguity, and partly because it would break the one-pass preprocessor into a recursive preprocessor.

That doesn't mean you can't do it, it just means you can't do it without going beyond the basic set of C++ tools, using either a meta-object protocol, a recursive preprocessor, or a simple extra preprocessor pass.

I'd still suggest trying to find a way to restate what you're trying to do. Not every design decision in C/C++ is a good one, but sometimes you can't do something because you really shouldn't need to. Unless you're trying to build a general-purpose library that will be used for purposes beyond your imagining, I'm not sure I see why you're trying to do what you are. However, you might want to look at what you get with #defining const definitions (and maybe templates) and whether that's close enough to replace #defining #defines. For the #include, I guess the closest you can come is conditional compilation around existing #includes, which isn't nearly as general as what you're looking for, but it still serves in many cases.

But, assuming that you absolutely need to do this, let's look at the options:

A meta-object protocol gives you full meta-language capabilities, far beyond a preprocessor, in an object-oriented way. The only usable MOP I know of for Visual C++ is OpenC++ (http://www.softlab.is.tsukuba.ac.jp/~chiba/openc++.html), but it's a great solution. It'll give you all of the functionality you want and more, although in a different way, and with a bit of a learning curve with this.

You could use a recursive preprocessor. I've seen a research project that implemented one, but you'd have to find it (I don't remember where it was) and port it from linux/gcc to Windows/VC.

A simpler way of doing the same thing is to write a script that just runs the standard preprocessor on your code repeatedly until nothing changes. However, be aware that the results may not be what you intuitively expect.

A final possibility is to use a simple pre-preprocessor step. You could just do two steps through the normal preprocessor, but in order to get what you want it may be better to explicitly separate our first-step macros from second-step macros.

Probably the easiest way to do this is to take gcc and modify it to use #macro instead of #define. Then use #macro for your meta-macros and other first-step macros, and #define for your second-step macros. Any #include steps inside first-step macros will not be processed until the second step (after being macro-expanded, which is what you want), while those which are not inside macros will be processed during the first step (so they'll get the benefit of your first-step macro processing).

So for each file, macro-preprocess the file (using gcc -i with your modified gcc) and then pass the results off to the VC compiler as ordinary C++ (or C) source. I'm not sure if you can use a custom build step to do this--you may have to modify the DSP file (generally a bad idea), use a makefile instead, or just run that separate step manually.

Then, for your code, instead of

#define MY_MACRO(a) \
  #define MY_CONSTANT_##a

#define MY_MACRO2(a) \
  #include #a

You'd use

#macro MY_MACRO(a) \
  #define MY_CONSTANT##(a)

#macro MY_MACRO2(a) \
  #include #a

LVL 11

Expert Comment

ID: 1324936
Good luck with your project.

B ekiM


Author Comment

ID: 1324937
Thanx PAYN,

I appreciate the time you took to answer my question.
I won't be doing all that stuff though, cause the goal is not worth the effort.
But thanx again


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question