views:

1918

answers:

4

Hi, I need a set of wrappers around the standard system calls-open,listen,etc. For these i have a few "#define" as in:

#define open(a,b,c) JCL_Open(a,b,c)

But when i compile the header and associated .c files, i am getting the following warning:

/jcl_wrappers.h:114:1: warning: "open" redefined
/jcl_wrappers.h:113:1: warning: this is the location of the previous definition

How can i suppress these warnings?

+4  A: 

Put include guards in your header file.

Basically you need to put these two lines at the beginning of your header file

#ifndef _yourincludefilename_h_
#define _yourincludefilename_h_

and

#endif /* _yourincludefilename_h_ */

at the end of you include file.

Edit:
rascher is right, that open is not a good name for a macro, as it will conflict with a library function. Usually it is good C convention to make macros all uppercase, so I would suggest to change your macro to

#define OPEN(a,b,c) JCL_Open(a,b,c)

or even better

#define XYZ_OPEN(a,b,c) JCL_Open(a,b,c)

where XYZ is a prefix specific to your code.

lothar
A: 

Wrap the define in:

#ifndef JCL_WRAPPERS_H
#define JCL_WRAPPERS_H

... your macro's

#endif

This makes sure your macro's are only defined once if you including your header in multiple places.

It's generally a smart thing to do for all prototypes in headers also.

Brian Gianforcaro
+2  A: 

Leave the standard functions alone and rename the function:

#define myopen(a,b,c) JCL_Open(a,b,c)

Someone will thank you later.

ojblass
+1  A: 

You might try using compile guards. Like:

my_headers.h:

#ifndef __MY_HEADERS
#define __MY_HEADERS

  #define open(a,b,c) JCL_Open(a,b,c)

#endif

This will only do do what is between the #ifndef and #endif if the '__MY_HEADERS' macro has been defined. So, everything in your .h file will only be declared once.

You could use the same construct to see if the "open" macro is already defined.

Also be aware that there is already a C function called open(): http://www.manpagez.com/man/2/open/ . May not be a great idea to use the same name for your macro!

rascher