473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

#include from #define macro???

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
May 31 '06 #1
13 15488

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.

May 31 '06 #2
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.
Jun 1 '06 #3
Rick Anderson <Ri**************@oracle.com> 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;}
Jun 1 '06 #4
>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
Jun 1 '06 #5
Michael Mair <Mi**********@invalid.invalid> 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
Jun 1 '06 #6
>
Why on earth would you want to do that?


What if he is not on Earth?
P.Krumins

Jun 1 '06 #7
On 31 May 2006 17:26:30 -0700, "Peteris Krumins"
<pe*************@gmail.com> 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
Jun 1 '06 #8
On Wed, 31 May 2006 22:23:49 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
On 31 May 2006 17:26:30 -0700, "Peteris Krumins"
<pe*************@gmail.com> 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
Jun 1 '06 #9

jaysome wrote:
On Wed, 31 May 2006 22:23:49 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
On 31 May 2006 17:26:30 -0700, "Peteris Krumins"
<pe*************@gmail.com> 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. :)

Jun 1 '06 #10
Gordon Burditt wrote:
[...]
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.


I was under the impression (both from experience, and asking here[1]) that
the above is not valid. Someone posted elsethread the same "solution",
and quoted C&V from C99, so perhaps it's valid C99, but not C90?
[1] I have hand-made config files for different platforms, and currently
use something along the lines of:

#if PLATFORM == "one"
#include <configs/one.h>
#elif PLATFORM == "two"
#include <configs/two.h>
#elif ...

I was hoping to define the filename to include (for example, using the
complier's "-DCONFIGFILE=<configs/whatever.h>") and then simply using:

#include CONFIGFILE

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jun 1 '06 #11
Kenneth Brody schrieb:
Gordon Burditt wrote:
[...]
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.


I was under the impression (both from experience, and asking here[1]) that
the above is not valid. Someone posted elsethread the same "solution",
and quoted C&V from C99, so perhaps it's valid C99, but not C90?


Nope. My C89 last public draft contains about the same text (3.8.2).
Both the draft and the C99 standard contain this text and example:

,---
A preprocessing directive of the form
# include pp-tokens new-line
(that does not match one of the two previous forms) is permitted. The
preprocessing tokens after include in the directive are processed just
as in normal text. (Each identifier currently defined as a macro name
is replaced by its replacement list of preprocessing tokens.) The
directive resulting after all replacements shall match one of the two
previous forms./77/ The method by which a sequence of preprocessing
tokens between a < and a > preprocessing token pair or a pair of
characters is combined into a single header name preprocessing token
is implementation-defined.

[...]

This example illustrates a macro-replaced #include directive:

#if VERSION == 1
#define INCFILE "vers1.h"
#elif VERSION == 2
#define INCFILE "vers2.h"
/* and so on */
#else
#define INCFILE "versN.h"
#endif
/*...*/
#include INCFILE
+----
77. Note that adjacent string literals are not concatenated into a
single string literal (see the translation phases in $2.1.1.2); thus
an expansion that results in two string literals is an invalid
directive.
`---

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 1 '06 #12
Kenneth Brody <ke******@spamcop.net> writes:
I was under the impression (both from experience, and asking here[1]) that
the above is not valid. Someone posted elsethread the same "solution",
and quoted C&V from C99, so perhaps it's valid C99, but not C90?


I commonly quote from C99 even when the feature is in C90, simply
because the final text of C99 is available in a format that may
be conveniently cut-and-pasted into a Usenet message, whereas C90
is not.

(Late drafts of C90 are available as plain text, but not the
final text, as far as I know.)
--
"Am I missing something?"
--Dan Pop
Jun 1 '06 #13
Michael Mair wrote:

Kenneth Brody schrieb:
Gordon Burditt wrote:
[...]
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.
I was under the impression (both from experience, and asking here[1]) that
the above is not valid. Someone posted elsethread the same "solution",
and quoted C&V from C99, so perhaps it's valid C99, but not C90?


Nope. My C89 last public draft contains about the same text (3.8.2).
Both the draft and the C99 standard contain this text and example:

[...] This example illustrates a macro-replaced #include directive:

#if VERSION == 1
#define INCFILE "vers1.h"
#elif VERSION == 2
#define INCFILE "vers2.h"
/* and so on */
#else
#define INCFILE "versN.h"
#endif
/*...*/
#include INCFILE

[...]

Well, I'll be. I know I tried that not too many years ago, and it
didn't work. I just tried it now, and it worked just fine.

Thanks.
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jun 2 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: yuri | last post by:
Hello, I wrote this simple stack: ---START--------------- #include "stackArr.h" #include "underflow.h" #include <iostream> StackArr::StackArr(int initialSize){
11
by: Marc Ferry | last post by:
I already posted this mail in comp.sys.hp and comp.sys.hp.hpux but had no response. As this problem might be present on other OSes than HP-UX 10.20, I crosspost it here, in the hope of getting an...
1
by: Ron | last post by:
Following the C++ standard document (ISO/IEC 14882) a preprocessing directive: #include pp-tokens new-line is permitted. So i can write #include "myfile.h" but i can use even an...
9
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
10
by: Joakim Hove | last post by:
Hello, I would like to construct a macro, which does conditional include of another source file. i.e. something like this: #MPIinclude(FileName) (#include (FileName)) /* Alternative one -...
9
by: zolli | last post by:
Hi, I've been banging my head against this for a while now. Hoping someone here can shed some light on what's going on. On including stdlib.h in a file, I'm seeing the following errors: ...
13
by: James | last post by:
I have a bunch of conditional #include statements like #if PLATFORM == "LINUX" #include "LinuxImplementation.h" #elif PLATFORM ==" WINDOWS" #include "WindowsImplementation.h" #elif PLATFORM...
6
by: Joseph Turian | last post by:
I am having difficulty defining a friend function because of a #include cycle: ============ obj.H ================ #ifndef _obj_ #define _obj_ #include "cont.H" class cont;
6
by: Bing | last post by:
Hi folks, Is there a way to define a macro that may contain #include directive in its body. If I just put the "#include", it gives error C2162: "expected macro formal parameter" since here I am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.