macro prototype

Discussion in 'C Programming' started by ashu, Feb 24, 2012.

  1. ashu

    ashu Guest

    Hi all,
    I want to use a macro which I want to define in a .c(lets call it
    macro.c) file instead of a .h(macro.h) file because it uses some local
    variables which are defined in the .c file. However I want this macro
    to be visible to other files which will include the
    corresponding .h(macro.h) file.

    The problem here is since the macro is defined in the macro.c file and
    there is no way I can declare a prototype of the macro in the macro.h
    file, this macro cannot be invoked by other files since the other
    files simply include the macro.h file.

    Is there anyway I can get around this problem ?

    regards
    Ashu
     
    ashu, Feb 24, 2012
    #1
    1. Advertising

  2. On Feb 24, 10:36 am, ashu <> wrote:

    > I want to use a macro which I want to define in a .c(lets call it
    > macro.c) file instead of a .h(macro.h) file because it uses some local
    > variables which are defined in the .c file.


    you cannot access local variables (I guessing you mean file scope?)
    using a macro used in another .c file (technical term "Translation
    Unit")

    > However I want this macro
    > to be visible to other files which will include the
    > corresponding .h(macro.h) file.
    >
    > The problem here is since the macro is defined in the macro.c file and
    > there is no way I can declare a prototype of the macro in the macro.h
    > file,


    since there is no such thing as a macro-prototype

    > this macro cannot be invoked by other files since the other
    > files simply include the macro.h file.
    >
    > Is there anyway I can get around this problem ?


    no. Not as stated you have a fundamental misconception about what
    macros are. macros (in C) do a simple textual substitution.

    Why can't your macro be a function? Or could your macro call a
    function that does the real work.

    please don't say "efficiency"...
     
    Nick Keighley, Feb 24, 2012
    #2
    1. Advertising

  3. ashu <> writes:

    > Hi all,
    > I want to use a macro which I want to define in a .c(lets call it
    > macro.c) file instead of a .h(macro.h) file because it uses some local
    > variables which are defined in the .c file. However I want this macro
    > to be visible to other files which will include the
    > corresponding .h(macro.h) file.
    >
    > The problem here is since the macro is defined in the macro.c file and
    > there is no way I can declare a prototype of the macro in the macro.h
    > file, this macro cannot be invoked by other files since the other
    > files simply include the macro.h file.
    >
    > Is there anyway I can get around this problem ?


    No. Even if you could solve the specific problem of the macro, you
    would still have a problem with the variable. Macros work by textual
    replacement, so a macro that uses a variable that is local to one file
    (in C terms it has internal linkage) will expand to code that references
    that variable where it is unavailable.

    Do you know you must use a macro? I'd just use a function. If that is
    really overkill, give the variable in question external linkage and
    declare it (extern ...) in the .h file along with the macro that uses
    it.

    --
    Ben.
     
    Ben Bacarisse, Feb 24, 2012
    #3
  4. ashu

    Eric Sosman Guest

    On 2/24/2012 5:36 AM, ashu wrote:
    > Hi all,
    > I want to use a macro which I want to define in a .c(lets call it
    > macro.c) file instead of a .h(macro.h) file because it uses some local
    > variables which are defined in the .c file. However I want this macro
    > to be visible to other files which will include the
    > corresponding .h(macro.h) file.
    >
    > The problem here is since the macro is defined in the macro.c file and
    > there is no way I can declare a prototype of the macro in the macro.h
    > file, this macro cannot be invoked by other files since the other
    > files simply include the macro.h file.
    >
    > Is there anyway I can get around this problem ?


    Not in the way you describe. Macros are not "declared" and
    do not have "prototypes;" they are defined (and perhaps undefined)
    and have definitions. Also, macros disappear at an early stage of
    compilation and are replaced by their expansions; there is no way
    for a macro to remain "visible" after it has been replaced.

    If you want the macro's expansion to refer to variables that
    live in a particular file, you can write the "macro.h" file like

    extern int variable;
    #define MACRO(x) ( variable += (x) )

    .... after which any source file that includes "macro.h" can write
    MACRO(millicent) or MACRO(42) or whatever. Meanwhile, the file
    that actually defines the variable should do

    #include "macro.h"
    int variable;

    .... and can, itself, also write MACRO(f(x)) if it wants.

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 24, 2012
    #4
  5. ashu

    ashu Guest

    On Feb 24, 1:49 pm, Eric Sosman <> wrote:
    > On 2/24/2012 5:36 AM, ashu wrote:
    >
    > > Hi all,
    > > I want to use a macro which I want to define in a .c(lets call it
    > > macro.c) file instead of a .h(macro.h) file because it uses some local
    > > variables which are defined in the .c file. However I want this macro
    > > to be visible to other files which will include the
    > > corresponding .h(macro.h) file.

    >
    > > The problem here is since the macro is defined in the macro.c file and
    > > there is no way I can declare a prototype of the macro in the macro.h
    > > file, this macro cannot be invoked by other files since the other
    > > files simply include the macro.h file.

    >
    > > Is there anyway I can get around this problem ?

    >
    >      Not in the way you describe.  Macros are not "declared" and
    > do not have "prototypes;" they are defined (and perhaps undefined)
    > and have definitions.  Also, macros disappear at an early stage of
    > compilation and are replaced by their expansions; there is no way
    > for a macro to remain "visible" after it has been replaced.
    >
    >      If you want the macro's expansion to refer to variables that
    > live in a particular file, you can write the "macro.h" file like
    >
    >         extern int variable;
    >         #define MACRO(x) ( variable += (x) )
    >
    > ... after which any source file that includes "macro.h" can write
    > MACRO(millicent) or MACRO(42) or whatever.  Meanwhile, the file
    > that actually defines the variable should do
    >
    >         #include "macro.h"
    >         int variable;
    >
    > ... and can, itself, also write MACRO(f(x)) if it wants.
    >
    > --
    > Eric Sosman
    >


    Thank you Nick Eric and Ben. It gave me lot of clarity!
    regards
    ashu
     
    ashu, Feb 24, 2012
    #5
  6. ashu

    Ya Shou Guest

    On Feb 24, 6:36 pm, ashu <> wrote:
    > Hi all,
    > I want to use a macro which I want to define in a .c(lets call it
    > macro.c) file instead of a .h(macro.h) file because it uses some local
    > variables which are defined in the .c file. However I want this macro
    > to be visible to other files which will include the
    > corresponding .h(macro.h) file.
    >
    > The problem here is since the macro is defined in the macro.c file and
    > there is no way I can declare a prototype of the macro in the macro.h
    > file, this macro cannot be invoked by other files since the other
    > files simply include the macro.h file.
    >
    > Is there anyway I can get around this problem ?
    >
    > regards
    > Ashu


    Why don't you separate, by moving, the macro definitions from the
    source into a header file?
    You can refer them by including and defining the variables in the
    source.
    And thus the your problem should be solved.

    By the way, please mind the macro guard in order to avoid the macro-
    multiple-definition problem.

    --
    Shou Ya.
     
    Ya Shou, Feb 25, 2012
    #6
    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. June Lee
    Replies:
    2
    Views:
    820
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    215
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    278
  5. javascript fish
    Replies:
    0
    Views:
    181
    javascript fish
    Oct 11, 2008
Loading...

Share This Page