Improve company productivity with a Business Account.Sign Up

x
?
Solved

#define, #include inside a MACRO

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

e.g.

#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.
0
Comment
Question by:jhendrix
  • 3
  • 2
6 Comments
 

Author Comment

by:jhendrix
ID: 1324932
Adjusted points to 50
0
 
LVL 11

Expert Comment

by:mikeblas
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

0
 

Author Comment

by:jhendrix
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.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LVL 1

Accepted Solution

by:
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

0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1324936
Good luck with your project.

B ekiM

0
 

Author Comment

by:jhendrix
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

jhendrix
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

587 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