3

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 Answers 4

5

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.

1
  • A lot of legacy source code use this trick especially to replace library functions with its own. lib9 in golang.org sources to name one. It would be nice to have a command line switch like -Wno-builtin-macro-redefined. I also failed to find it.
    – user222202
    Aug 23, 2011 at 15:54
5

Leave the standard functions alone and rename the function:

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

Someone will thank you later.

1

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!

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.