#include from #define macro???

Discussion in 'C Programming' started by swengineer001@gmail.com, Jun 1, 2006.

  1. Guest

    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.
     
    , Jun 1, 2006
    #1
    1. Advertising

  2. 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
     
    Rick Anderson, Jun 1, 2006
    #2
    1. Advertising

  3. Michael Mair Guest

    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.
     
    Michael Mair, Jun 1, 2006
    #3
  4. Ben Pfaff Guest

    Rick Anderson <> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
     
    Ben Pfaff, Jun 1, 2006
    #4
  5. >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
     
    Gordon Burditt, Jun 1, 2006
    #5
  6. >
    > Why on earth would you want to do that?
    >


    What if he is not on Earth?


    P.Krumins
     
    Peteris Krumins, Jun 1, 2006
    #6
  7. Michael Mair <> 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
     
    Rick Anderson, Jun 1, 2006
    #7
  8. Jack Klein Guest

    Jack Klein, Jun 1, 2006
    #8
  9. jaysome Guest

    On Wed, 31 May 2006 22:23:49 -0500, Jack Klein <>
    wrote:

    >On 31 May 2006 17:26:30 -0700, "Peteris Krumins"
    ><> 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/introduction/index.html#three


    :^)

    --
    jaysome
     
    jaysome, Jun 1, 2006
    #9
  10. Guest

    jaysome wrote:
    > On Wed, 31 May 2006 22:23:49 -0500, Jack Klein <>
    > wrote:
    >
    > >On 31 May 2006 17:26:30 -0700, "Peteris Krumins"
    > ><> 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, 2006
    #10
  11. 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:>
     
    Kenneth Brody, Jun 1, 2006
    #11
  12. Michael Mair Guest

    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.
     
    Michael Mair, Jun 1, 2006
    #12
  13. Ben Pfaff Guest

    Kenneth Brody <> 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
     
    Ben Pfaff, Jun 2, 2006
    #13
  14. 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:>
     
    Kenneth Brody, Jun 2, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,135
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    592
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    515
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    492
    Patrick Kowalzick
    Mar 14, 2006
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    967
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page