Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > macro prototype

Reply
Thread Tools

macro prototype

 
 
ashu
Guest
Posts: n/a
 
      02-24-2012
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

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      02-24-2012
On Feb 24, 10:36*am, ashu <(E-Mail Removed)> 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"...
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-24-2012
ashu <(E-Mail Removed)> 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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-24-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
ashu
Guest
Posts: n/a
 
      02-24-2012
On Feb 24, 1:49*pm, Eric Sosman <(E-Mail Removed)> 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
> (E-Mail Removed)


Thank you Nick Eric and Ben. It gave me lot of clarity!
regards
ashu
 
Reply With Quote
 
Ya Shou
Guest
Posts: n/a
 
      02-25-2012
On Feb 24, 6:36*pm, ashu <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prototype WTP 0.2 released,this release for Prototype 1.6.0 javascript fish Javascript 0 10-11-2008 07:35 AM
Class prototype vs C function prototype June Lee C++ 2 04-13-2008 08:17 PM
Prototype Object.extend(new Base() | Hash | Hash.prototype) usage: jacobstr@gmail.com Javascript 3 03-27-2007 07:56 AM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM



Advertisments