#include from #define macro???

S

swengineer001

Rick said:
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.
 
R

Rick Anderson

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
 
M

Michael Mair

Rick said:
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
 
B

Ben Pfaff

Rick Anderson said:
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.
 
G

Gordon Burditt

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
 
R

Rick Anderson

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
 
K

Kenneth Brody

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:[email protected]>
 
M

Michael Mair

Kenneth said:
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
 
B

Ben Pfaff

Kenneth Brody said:
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.)
 
K

Kenneth Brody

Michael said:
Kenneth said:
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:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top