#define a macro that #includes

Discussion in 'C Programming' started by .rhavin grobert, Apr 10, 2008.

  1. first of all: sorry for perhaps posting something already answered
    anywhere, i couldnt find any useful info - define, macro, include and
    so on are very poor keywords;-)

    my problem:

    i want to do something like...

    INCLUDE_TYPES(#include);

    ....that resolves to ...

    #include "file1.h"
    #include "file2.h"
    #include "file3.h"


    .... and my try was a...

    #define INCLUDE_TYPES(Q_L) \
    Q_L "file1.h" \
    Q_L "file2.h" \
    Q_L "file3.h" \

    ....but my compiler complains about...

    '#' : invalid character : possibly the result of a macro expansion

    ....so that way dont work. i cant do a simple...

    #include "files.h"

    ....with all other includes in that file so im looking for some
    different idea and would appreciate any useful help.

    TIA, -.rhavin;-)
     
    .rhavin grobert, Apr 10, 2008
    #1
    1. Advertising

  2. .rhavin grobert

    jxh Guest

    On Apr 10, 10:41 am, ".rhavin grobert" <> wrote:
    >
    > my problem:
    >
    > i want to do something like...
    >
    > INCLUDE_TYPES(#include);
    >
    > ...that resolves to ...
    >
    > #include "file1.h"
    > #include "file2.h"
    > #include "file3.h"
    >
    > ... and my try was a...
    >
    > #define INCLUDE_TYPES(Q_L) \
    > Q_L "file1.h" \
    > Q_L "file2.h" \
    > Q_L "file3.h" \
    >
    > ...but my compiler complains about...
    >
    >  '#' : invalid character : possibly the result of a macro
    > expansion
    >
    > ...so that way dont work.


    Right. You can't expand a preprocessor macro into a
    preprocessor directive. You can use the presence of
    a macro to determine whether or not to include your
    header files by:

    #ifdef INCLUDE_TYPES
    # include "file1.h"
    # include "file2.h"
    # include "file3.h"
    #endif

    > i cant do a simple...
    >
    > #include "files.h"
    >
    > ...with all other includes in that file so im looking for some
    > different idea and would appreciate any useful help.
    >


    You need to explain why you can't do that.

    -- James
     
    jxh, Apr 10, 2008
    #2
    1. Advertising

  3. In article <>,
    ..rhavin grobert <> wrote:

    >my problem:


    >i want to do something like...


    >INCLUDE_TYPES(#include);


    >...that resolves to ...


    >#include "file1.h"
    >#include "file2.h"
    >#include "file3.h"


    You cannot do that. No macro can expand to a preprocessor directive.
    --
    "Walter is a great man." -- Dennis Green
     
    Walter Roberson, Apr 10, 2008
    #3
  4. On Thu, 10 Apr 2008 10:41:30 -0700, .rhavin grobert wrote:
    > first of all: sorry for perhaps posting something already answered
    > anywhere, i couldnt find any useful info - define, macro, include and so
    > on are very poor keywords;-)
    >
    > my problem:
    >
    > i want to do something like...
    >
    > INCLUDE_TYPES(#include);
    >
    > ...that resolves to ...
    >
    > #include "file1.h"
    > #include "file2.h"
    > #include "file3.h"


    That's not possible in C. Macro expansions cannot ever generate
    preprocessing directives.

    > ... and my try was a...
    >
    > #define INCLUDE_TYPES(Q_L) \
    > Q_L "file1.h" \
    > Q_L "file2.h" \
    > Q_L "file3.h" \
    >
    > ...but my compiler complains about...
    >
    > '#' : invalid character : possibly the result of a macro expansion


    Right. Since #include doesn't appear at the start of a line in your code,
    it's not a directive, and the # is left and causes syntax errors.

    > ...so that way dont work. i cant do a simple...
    >
    > #include "files.h"
    >
    > ...with all other includes in that file so im looking for some different
    > idea and would appreciate any useful help.


    Why can't you do that? If you have a problem you want to solve, ask about
    that problem. Your attempted solution doesn't work and can't be made to
    work. Asking how to change that solution so that it will work won't get
    you useful answers. Asking how to solve your original problem may.
     
    Harald van Dijk, Apr 10, 2008
    #4
  5. On 10 Apr., 20:15, jxh <> wrote:
    > On Apr 10, 10:41 am, ".rhavin grobert" <> wrote:
    > > my problem:
    > > i want to do something like...
    > > INCLUDE_TYPES(#include);
    > > ...that resolves to ...
    > > #include "file1.h"
    > > #include "file2.h"
    > > #include "file3.h"
    > > ... and my try was a...
    > > #define INCLUDE_TYPES(Q_L) \
    > > Q_L "file1.h" \
    > > Q_L "file2.h" \
    > > Q_L "file3.h" \
    > > ...but my compiler complains about...
    > > '#' : invalid character : possibly the result of a macro
    > > expansion
    > > ...so that way dont work.

    > Right. You can't expand a preprocessor macro into a
    > preprocessor directive. You can use the presence of
    > a macro to determine whether or not to include your
    > header files by:
    > #ifdef INCLUDE_TYPES
    > # include "file1.h"
    > # include "file2.h"
    > # include "file3.h"
    > #endif
    > > i cant do a simple...
    > > #include "files.h"
    > > ...with all other includes in that file so im looking for some
    > > different idea and would appreciate any useful help.

    > You need to explain why you can't do that.


    i have a pack that consists of 3 different classes meant for
    deriviation by the client (yes, thats c++)

    they client may specify differnt additional classes and they shall be
    included, so i created a file that is a headerfile and consists of
    certain macros allowing the client to modify the package. and wantet
    to find a solution to have it all in one configuration file, one part
    of the config file looks for example like this:
    ____________________________________________


    //-----------------------------------------------------------------------------
    // list types' corresponding info-classes and file-classes
    // usage: Q_T([FILETYPE]) Q_I([Header-Class]) Q_F([File-Class]) Q_B
    // escape EOLs (lineends) to have a single #define-line
    #define QFILEMANAGER_TYPECLASSES(Q_T, Q_I, Q_F, Q_B) \
    Q_T(FZT_RECORD) Q_I(CInfRec) Q_F(CFZRecord) Q_B \
    Q_T(FZT_POOL) Q_I(CInfPool) Q_F(CFZPool) Q_B \
    Q_T(FZT_DEFN) Q_I(CInfDef) Q_F(CFZDef) Q_B \
    Q_T(FZT_BASE) Q_I(CInfBase) Q_F(CFZBase) Q_B \
    Q_T(FZT_LOCAL) Q_I(CInfLocal) Q_F(CFZLocal) Q_B \
    Q_T(FZT_STUDY) Q_I(CInfStudy) Q_F(CFZStudy) Q_B \

    ____________________________________________

    in my pack i have the following definitions

    #define QFM_NOP(s)
    #define QFM_EMPTY
    #define QFM_CASE(s) case s:
    #define QFM_NEW(s) new s; break;
    #define QFM_DELETE(s) delete (s*)
    #define QFM_BREAK ;break;

    ....by now 'calling' the above macro with the following code in my
    class...

    switch (type) {
    QFILEMANAGER_TYPECLASSES(QFM_CASE, QFM_NOP, pFile = QFM_NEW,
    QFM_EMPTY);
    default:
    //
    }

    ....i get my allocator and by calling it that way ...

    switch (pInfo->TypeGet()) {
    QFILEMANAGER_TYPECLASSES(QFM_CASE, QFM_DELETE, QFM_NOP, pInfo
    QFM_BREAK);
    default:
    return false;
    }

    ....i delocate (correct word?) it. But that definition file is included
    in many files in the pack where i dont want to have the #include-
    sections, so im looking for a way to have a....

    //-------------------------------------
    // put yout file pathes here
    MYMACRO(somewhat) \
    "filepath" \
    "filepath" \

    ....and decide in every file where i include the user-config-header if
    i want to also include his specified files. the...

    #ifdef INCLUDE_TYPES
    # include "file1.h"
    # include "file2.h"
    # include "file3.h"
    #endif

    ....seems to be a possibility, so'll try that, thx, .rhavin
     
    .rhavin grobert, Apr 10, 2008
    #5
  6. ".rhavin grobert" <> writes:

    > first of all: sorry for perhaps posting something already answered
    > anywhere, i couldnt find any useful info - define, macro, include and
    > so on are very poor keywords;-)
    >
    > my problem:
    >
    > i want to do something like...
    >
    > INCLUDE_TYPES(#include);
    >
    > ...that resolves to ...
    >
    > #include "file1.h"
    > #include "file2.h"
    > #include "file3.h"
    >
    >
    > ... and my try was a...
    >
    > #define INCLUDE_TYPES(Q_L) \
    > Q_L "file1.h" \
    > Q_L "file2.h" \
    > Q_L "file3.h" \
    >
    > ...but my compiler complains about...
    >
    > '#' : invalid character : possibly the result of a macro expansion


    The result of a macro expansion can't be a pre-processing directive --
    no matter how much it looks like one. No macro can generate a
    #include, #define etc. You can generate those tokens, but they are
    not treated as directives.

    > ...so that way dont work. i cant do a simple...
    >
    > #include "files.h"
    >
    > ...with all other includes in that file so im looking for some
    > different idea and would appreciate any useful help.


    Backup a bit. What problem were you trying to solve?

    --
    Ben.
     
    Ben Bacarisse, Apr 10, 2008
    #6
  7. .rhavin grobert

    jxh Guest

    On Apr 10, 11:41 am, ".rhavin grobert" <> wrote:
    > On 10 Apr., 20:15, jxh <> wrote:
    > > On Apr 10, 10:41 am, ".rhavin grobert" <> wrote:
    > > > i cant do a simple...
    > > > #include "files.h"
    > > > ...with all other includes in that file so im looking for some
    > > > different idea and would appreciate any useful help.

    > > You need to explain why you can't do that.

    >
    > i have a pack that consists of 3 different classes meant for
    > deriviation by the client (yes, thats c++)
    >
    > they client may specify differnt additional classes and they shall be
    > included, so i created a file that is a headerfile and consists of
    > certain macros allowing the client to modify the package.

    [...]

    It doesn't seem to me you are leveraging the full capabilities of C++.
    If you describe your general architecture problem of accessing user
    defined helper classes from your library's base class, I am sure you
    would get good answers from comp.lang.c++.

    -- James
     
    jxh, Apr 10, 2008
    #7
  8. -cnrc.gc.ca (Walter Roberson) wrote:
    > .rhavin grobert <> wrote:
    > >my problem:
    > >i want to do something like...
    > >INCLUDE_TYPES(#include);
    > >...that resolves to ...
    > >#include "file1.h"
    > >#include "file2.h"
    > >#include "file3.h"

    >
    > No macro can expand to a preprocessor directive.


    True, but preprocessing directives, including #include,
    can expand to macros.

    It's possible to do something like what the OP wants
    (upto a static limit), but it's questionable as to
    whether it's worth doing. Since the OP has admitted
    to using C++, it's more likely there are better C++
    paradigms to achieve what they really need to do.

    But there is a Boost header that does many surprising
    pre-processor tricks, and is both C and C++ compatible.

    --
    Peter
     
    Peter Nilsson, Apr 11, 2008
    #8
    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,132
    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:
    590
    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:
    512
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    485
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    474
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page