Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > #include from #define macro???

Reply
Thread Tools

#include from #define macro???

 
 
swengineer001@gmail.com
Guest
Posts: n/a
 
      05-31-2006

Rick Anderson wrote:
> Group,
>
> I want to define a #include directive from another macro,
> if possible...
>
> For example, the following doesn't work but is basically
> what I need to do:
>
> #define INCLUDE_THIS(header_name) #include header_name
>
> so that when a developer does the following in the code
>
> INCLUDE_THIS("this_file.h")
>
> the header file "this_file.h" gets included into the file.
>
> Any suggestions???
>
> Thanks in advance!
> Rick


What is stopping the developer from putting #include "this_file.h"
instead? I am not seeing what you would gain from doing this even if
you could.

 
Reply With Quote
 
 
 
 
Rick Anderson
Guest
Posts: n/a
 
      05-31-2006
Group,

I want to define a #include directive from another macro,
if possible...

For example, the following doesn't work but is basically
what I need to do:

#define INCLUDE_THIS(header_name) #include header_name

so that when a developer does the following in the code

INCLUDE_THIS("this_file.h")

the header file "this_file.h" gets included into the file.

Any suggestions???

Thanks in advance!
Rick
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      06-01-2006
Rick Anderson schrieb:
> I want to define a #include directive from another macro,
> if possible...


It is not. You cannot generate preprocessing directives
using #define as "#" is an operator with special meaning
within macro definitions.
What you _can_ do, according to C99 6.10.2#4, is
#include MY_INCLUDE
where MY_INCLUDE expands to "Header" or <Header>.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      06-01-2006
Rick Anderson <(E-Mail Removed)> writes:

> I want to define a #include directive from another macro,
> if possible...
>
> For example, the following doesn't work but is basically
> what I need to do:
>
> #define INCLUDE_THIS(header_name) #include header_name
>
> so that when a developer does the following in the code
>
> INCLUDE_THIS("this_file.h")
>
> the header file "this_file.h" gets included into the file.


That won't work because macro expansions are not treated as
preprocessor directives.

However, the argument to #include will be macro-expanded if it
does not match either of the conventional "..." or <...> forms.
This might work in your case, but it's hard to tell without more
information.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      06-01-2006
>I want to define a #include directive from another macro,
>if possible...


The expansion of a macro is not processed as a preprocessor
directive, even if it looks like one.

>For example, the following doesn't work but is basically
>what I need to do:
>
>#define INCLUDE_THIS(header_name) #include header_name
>
>so that when a developer does the following in the code
>
>INCLUDE_THIS("this_file.h")


Yeeecccchhh!!

Why on earth would you want to do that?

>the header file "this_file.h" gets included into the file.
>
>Any suggestions???


Please do not distribute code like that to anyone.

You can do:

#define INCLUDE_THIS_FILE "god.awful.mess"
#include INCLUDE_THIS_FILE

but I don't see a whole lot of use for it.

Gordon L. Burditt
 
Reply With Quote
 
Peteris Krumins
Guest
Posts: n/a
 
      06-01-2006
>
> Why on earth would you want to do that?
>


What if he is not on Earth?


P.Krumins

 
Reply With Quote
 
Rick Anderson
Guest
Posts: n/a
 
      06-01-2006
Michael Mair <(E-Mail Removed)> wrote in news:4e6p7gF1df4m9U1
@individual.net:

> It is not. You cannot generate preprocessing directives
> using #define as "#" is an operator with special meaning
> within macro definitions.
> What you _can_ do, according to C99 6.10.2#4, is
> #include MY_INCLUDE
> where MY_INCLUDE expands to "Header" or <Header>.


Thanks! I thought as not...

Rick
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      06-01-2006
On 31 May 2006 17:26:30 -0700, "Peteris Krumins"
<(E-Mail Removed)> wrote in comp.lang.c:

> >
> > Why on earth would you want to do that?
> >

>
> What if he is not on Earth?
>
>
> P.Krumins


The International Organization for Standards (ISO) claims no
jurisdiction beyond the planet.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      06-01-2006
On Wed, 31 May 2006 22:23:49 -0500, Jack Klein <(E-Mail Removed)>
wrote:

>On 31 May 2006 17:26:30 -0700, "Peteris Krumins"
><(E-Mail Removed)> wrote in comp.lang.c:
>
>> >
>> > Why on earth would you want to do that?
>> >

>>
>> What if he is not on Earth?
>>
>>
>> P.Krumins

>
>The International Organization for Standards (ISO) claims no
>jurisdiction beyond the planet.


Neither does the International Organization for Standardization (ISO).

http://www.iso.org/iso/en/aboutiso/i...dex.html#three


:^)

--
jaysome
 
Reply With Quote
 
swengineer001@gmail.com
Guest
Posts: n/a
 
      06-01-2006

jaysome wrote:
> On Wed, 31 May 2006 22:23:49 -0500, Jack Klein <(E-Mail Removed)>
> wrote:
>
> >On 31 May 2006 17:26:30 -0700, "Peteris Krumins"
> ><(E-Mail Removed)> wrote in comp.lang.c:
> >
> >> >
> >> > Why on earth would you want to do that?
> >> >
> >>
> >> What if he is not on Earth?
> >>
> >>
> >> P.Krumins

> >
> >The International Organization for Standards (ISO) claims no
> >jurisdiction beyond the planet.

>
> Neither does the International Organization for Standardization (ISO).
>

But the Interplanetary Organization for Standarization does.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments