preprocessor macro problem

Discussion in 'C Programming' started by Erik Arle, Jan 30, 2011.

  1. Erik Arle

    Erik Arle Guest

    Hi,
    I want to have a macro that includes a bunch of
    include files and functions into my code when called.
    Here is an example:

    #define MY_MACRO() #include <a.h>\
    #include <b.h>\
    ...
    f()\
    {\
    printf("Hello\n");\

    }\

    This is not syntacally right since the "#" before the include
    is interpreted as a stringizig char and an error is flagged by
    the compiler. Also, all the code is placed n a single line,
    the includes flag an error. How can you write a macro like this??
    I have a large number of files in which I need to include
    code like this. Any help is greatly appreciated.
     
    Erik Arle, Jan 30, 2011
    #1
    1. Advertising

  2. Erik Arle

    none Guest

    In article <ii4dc4$rp2$>,
    Erik Arle <> wrote:
    >Hi,
    > I want to have a macro that includes a bunch of
    > include files and functions into my code when called.
    > Here is an example:
    >
    >#define MY_MACRO() #include <a.h>\
    >#include <b.h>\
    >..
    >f()\
    >{\
    > printf("Hello\n");\
    >
    >}\


    Here is a suggestion. Let's say your file is myfile.c.
    Make a file myfile-includes.c that has all the material
    that you want to include. Then in myfile.c put

    #include "myfile-includes.c"

    --
    rr
     
    none, Jan 30, 2011
    #2
    1. Advertising

  3. Erik Arle

    Eric Sosman Guest

    On 1/30/2011 2:15 PM, Erik Arle wrote:
    > Hi,
    > I want to have a macro that includes a bunch of
    > include files and functions into my code when called.
    > Here is an example:
    >
    > #define MY_MACRO() #include<a.h>\
    > #include<b.h>\
    > ..
    > f()\
    > {\
    > printf("Hello\n");\
    >
    > }\
    >
    > This is not syntacally right since the "#" before the include
    > is interpreted as a stringizig char and an error is flagged by
    > the compiler. Also, all the code is placed n a single line,
    > the includes flag an error. How can you write a macro like this??


    Only by using some other language than C. In C, macro
    expansion cannot produce a preprocessor directive, even if it
    yields a sequence of tokens that look like one.

    > I have a large number of files in which I need to include
    > code like this. Any help is greatly appreciated.


    Without knowing what you "need" and why, I can't offer much
    besides generic suggestions. The main one is to jettison MY_MACRO
    altogether, since even if it worked it wouldn't be very useful.
    (You couldn't use it more than once in a source file, and you
    need to #define it someplace, and if you're using it in "a large
    number of files" you'll probably #define it in a header, and if
    you're going to #include that header it might as well #include the
    others without all this running around.)

    If you've got a use case that you think isn't addressed by a
    perfectly ordinary #include, please explain the situation more
    fully. It's quite likely that someone can suggest a solution if
    you'll describe your problem.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 30, 2011
    #3
  4. Erik Arle <> wrote:
    > Hi,
    >         I want to have a macro that includes a bunch of
    >         include files and functions into my code when called.


    Whatever your problem, your attempted solution is wrong in many
    ways. I'm reasonably sure there is a solution, but you should
    mention what the actual problem is to get the right one.

    >         Here is an example:
    >
    > #define MY_MACRO() #include <a.h>\
    > #include <b.h>\
    > ..
    > f()\
    > {\
    >         printf("Hello\n");\
    >
    > }\


    There are many acceptable ways of performing conditional
    inclusion, but macros that define functions are relatively
    rare, especially if the macro takes no parameters.

    The function definition can only exist once in the entire
    program, so what is the point of making it a macro?

    > This is not syntacally right ...


    Indeed.

    > How can you write a macro like this??


    Why are you trying to is a better question.

    > I have a large number of files in which I need to include
    > code like this.


    I seriously doubt you need to include code like that. Rather
    than describing your ideal solution, you should show us the
    real problem. Don't be frightened to put in less contrived
    code.

    --
    Peter
     
    Peter Nilsson, Jan 31, 2011
    #4
    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. qazmlp
    Replies:
    18
    Views:
    560
    Martin Dickopp
    Aug 16, 2003
  2. Cronus
    Replies:
    1
    Views:
    676
    Paul Mensonides
    Jul 15, 2004
  3. qazmlp
    Replies:
    18
    Views:
    845
    Martin Dickopp
    Aug 16, 2003
  4. Charlie Zender

    C-preprocessor macro question

    Charlie Zender, Dec 24, 2003, in forum: C Programming
    Replies:
    3
    Views:
    460
    Richard Bos
    Dec 24, 2003
  5. Eric

    preprocessor q: impossible macro?

    Eric, Jul 18, 2005, in forum: C Programming
    Replies:
    21
    Views:
    659
Loading...

Share This Page