bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: filemode with C++


From: Bruno Haible
Subject: Re: filemode with C++
Date: Fri, 14 Jan 2011 08:43:58 +0100
User-agent: KMail/1.9.9

Hi John,

> I'd like to use the filemode module in a C++ file but found that it
> does not have extern "C" magic.  I started to submit a patch, but
> stopped when I saw that filemode.h has
> 
>   # if HAVE_DECL_STRMODE
>   #  include <string.h> /* FreeBSD, OpenBSD */
>   #  include <unistd.h> /* NetBSD */
>   # else
>   void strmode (mode_t mode, char *str);
>   # endif
> 
>   void filemodestring (struct stat const *statp, char *str);
> 
> and remembered there is a recommendation in the gnulib manual to not
> surround system header files with an extern "C" block.
> 
> What's the best way to get declare these two functions extern "C" and
> be consistent with gnulib conventions?

The best way is to first include all necessary system header files, then,
in an extern "C" block, do all variable or function declarations.

It is OK to duplicate a #if condition for this purpose.

> Is it safe to assume that if HAVE_DECL_STRMODE is true, strmode will
> be declared extern "C"?

Yes. I checked

MacOS X 10.3 <string.h>
FreeBSD 6.4 <string.h>
OpenBSD 3.8 <string.h>
NetBSD 3.0 <unistd.h>

and they all look like this:

__BEGIN_DECLS
...
void     strmode(mode_t, char *);
...
__END_DECLS

In general, nowadays, it's rare to find a system function that is _not_
properly declared inside extern "C" for C++. (Rare example: tcgetsid()
on HP-UX 11.00.)

Here's a proposed patch. In passing, this patch adds an 'extern' modifier',
which is redundant for functions, but nice to have
  - for consistency between variable and function declarations,
  - for consistency with the style of the glibc header files,
  - for determining the desired set of exported symbols of a shared library
    from its include files.

Jim, OK to commit?


2011-01-14  Bruno Haible  <address@hidden>

        filemode: Make function declarations usable in C++ mode.
        * lib/filemode.h: Enclose function declarations in extern "C" block.
        Reported by John W. Eaton <address@hidden>.

--- lib/filemode.h.orig Fri Jan 14 08:32:57 2011
+++ lib/filemode.h      Fri Jan 14 08:23:40 2011
@@ -21,13 +21,24 @@
 # include <sys/types.h>
 # include <sys/stat.h>
 
+/* Get the declaration of strmode.  */
 # if HAVE_DECL_STRMODE
-#  include <string.h> /* FreeBSD, OpenBSD */
+#  include <string.h> /* MacOS X, FreeBSD, OpenBSD */
 #  include <unistd.h> /* NetBSD */
-# else
-void strmode (mode_t mode, char *str);
 # endif
 
-void filemodestring (struct stat const *statp, char *str);
+# ifdef __cplusplus
+extern "C" {
+# endif
+
+# if !HAVE_DECL_STRMODE
+extern void strmode (mode_t mode, char *str);
+# endif
+
+extern void filemodestring (struct stat const *statp, char *str);
+
+# ifdef __cplusplus
+}
+# endif
 
 #endif



reply via email to

[Prev in Thread] Current Thread [Next in Thread]