Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   preprocessor macro problem (http://www.velocityreviews.com/forums/t742834-preprocessor-macro-problem.html)

Erik Arle 01-30-2011 07:15 PM

preprocessor macro problem
 
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??
I have a large number of files in which I need to include
code like this. Any help is greatly appreciated.

none 01-30-2011 07:26 PM

Re: preprocessor macro problem
 
In article <ii4dc4$rp2$1@speranza.aioe.org>,
Erik Arle <nospam@nospam.com> 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");\
>
>}\


Here is a suggestion. Let's say your file is myfile.c.
Make a file myfile-includes.c that has all the material
that you want to include. Then in myfile.c put

#include "myfile-includes.c"

--
rr

Eric Sosman 01-30-2011 08:25 PM

Re: preprocessor macro problem
 
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
esosman@ieee-dot-org.invalid

Peter Nilsson 01-31-2011 01:37 AM

Re: preprocessor macro problem
 
Erik Arle <nos...@nospam.com> wrote:
> Hi,
> * * * * I want to have a macro that includes a bunch of
> * * * * include files and functions into my code when called.


Whatever your problem, your attempted solution is wrong in many
ways. I'm reasonably sure there is a solution, but you should
mention what the actual problem is to get the right one.

> * * * * Here is an example:
>
> #define MY_MACRO() #include <a.h>\
> #include <b.h>\
> ..
> f()\
> {\
> * * * * printf("Hello\n");\
>
> }\


There are many acceptable ways of performing conditional
inclusion, but macros that define functions are relatively
rare, especially if the macro takes no parameters.

The function definition can only exist once in the entire
program, so what is the point of making it a macro?

> This is not syntacally right ...


Indeed.

> How can you write a macro like this??


Why are you trying to is a better question.

> I have a large number of files in which I need to include
> code like this.


I seriously doubt you need to include code like that. Rather
than describing your ideal solution, you should show us the
real problem. Don't be frightened to put in less contrived
code.

--
Peter


All times are GMT. The time now is 01:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.