[splint-discuss] glib header and empty #define's

Michael Wojcik Michael.Wojcik at microfocus.com
Wed Feb 27 08:37:47 PST 2008


> From: splint-discuss-bounces at cs.virginia.edu 
> [mailto:splint-discuss-bounces at cs.virginia.edu] On Behalf Of 
> Jack T Mudge III
> Sent: Tuesday, 26 February, 2008 18:19
> 
> in /usr/include/glib-2.0/glib/gmacros.h
> /* Guard C code in headers, while including them from C++ */
> #ifdef  __cplusplus
> # define G_BEGIN_DECLS  extern "C" {
> # define G_END_DECLS    }
> #else
> # define G_BEGIN_DECLS ;
> # define G_END_DECLS ;
> #endif
> 
> The point of this snippet seems to be to  keep some 
> declarations from causing problems for C++ compilers, which 
> is fine.

Not quite.

C++ implementations will define "__cplusplus", so when this header is
processed by a C++ implementation, G_BEGIN_DECLS will be defined as a
macro that expands to "extern "C" {", and G_END_DECLS will be defined as
a macro that expands to "}".

extern "C" is C++ syntax that tells the implementation that the
following declaration, or bracketted series of declarations, use the "C"
linkage convention. It's used to tell C++ that the declarations refer to
non-C++ code, for reasons that are irrelevant if you're not using C++.

splint is not a C++ implementation, so it should not have __cplusplus
defined (and won't, unless your configuration or one of the files you're
processing erroneously defines it).

> The definition G_BEGIN_DECLS and G_END_DECLS, then, should be "" in my

> source file -- __cplusplus is NOT defined.

No. Read the source again. If __cplusplus is not defined, then
G_BEGIN_DECLS and G_END_DECLS are both defined as macros that expand to
a semicolon - NOT an empty string.

(I have no idea why the author of that header has them expand to
semicolons; I can't offhand think of any situation where a linkage
declaration would be appropriate in C++ and a semicolon would be needed
in C. But a lot of GNU code is rather poor C.)

> /usr/include/glib/gtypes.h:41:8: Parse Error: Non-function 
> declaration:
>     G_BEGIN_DECLS : int. (For help on parse errors, see splint -help
>     parseerrors.)

This implies that you haven't included gmacros.h at all, or it would
never see a token named G_BEGIN_DECLS.

> I have tried adding #define G_BEGIN_DECLS ; to my source file, and
splint 
> complains about a duplicate definition

It's likely, then, that you have gtypes.h being included before
gmacros.h, so the definition of the G_BEGIN_DECLS macro is only
encountered after Splint tries to process gtypes.h.

Can you produce a small example that demonstrates the problem?

-- 
Michael Wojcik
Principal Software Systems Developer, Micro Focus



More information about the splint-discuss mailing list