On 1/30/2011 2:15 PM, Erik Arle wrote:
> Hi,
> I want to have a macro that includes a bunch of
> include files and functions into my code when called.
> Here is an example:
>
> #define MY_MACRO() #include<a.h>\
> #include<b.h>\
> ..
> f()\
> {\
> printf("Hello\n");\
>
> }\
>
> This is not syntacally right since the "#" before the include
> is interpreted as a stringizig char and an error is flagged by
> the compiler. Also, all the code is placed n a single line,
> the includes flag an error. How can you write a macro like this??
Only by using some other language than C. In C, macro
expansion cannot produce a preprocessor directive, even if it
yields a sequence of tokens that look like one.
> I have a large number of files in which I need to include
> code like this. Any help is greatly appreciated.
Without knowing what you "need" and why, I can't offer much
besides generic suggestions. The main one is to jettison MY_MACRO
altogether, since even if it worked it wouldn't be very useful.
(You couldn't use it more than once in a source file, and you
need to #define it someplace, and if you're using it in "a large
number of files" you'll probably #define it in a header, and if
you're going to #include that header it might as well #include the
others without all this running around.)
If you've got a use case that you think isn't addressed by a
perfectly ordinary #include, please explain the situation more
fully. It's quite likely that someone can suggest a solution if
you'll describe your problem.
--
Eric Sosman
lid