macro prototype

A

ashu

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
 
N

Nick Keighley

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"...
 
B

Ben Bacarisse

ashu said:
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.
 
E

Eric Sosman

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.
 
A

ashu

     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.

Thank you Nick Eric and Ben. It gave me lot of clarity!
regards
ashu
 
Y

Ya Shou

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top