This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: gcc gets in the way of writing C that is also C++ wrt const
- From: Jay K <jay dot krell at cornell dot edu>
- To: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 21 Jul 2009 13:50:01 +0000
- Subject: RE: gcc gets in the way of writing C that is also C++ wrt const
I was pointed to:
http://david.tribble.com/text/cdiffs.htm#C99-const-linkage
which recommends exactly what I'd like to do:
>> The recommended practice is therefore to define constants with an explicit static or extern specifier
C:\>type 1.c
extern const int b = 1;
C:\>gcc -c 1.c
1.c:1: warning: `b' initialized and declared `extern'
Why the warning?
- Jay
----------------------------------------
> From: jay.krell@cornell.edu
> To: gcc-help@gcc.gnu.org
> Subject: gcc gets in the way of writing C that is also C++ wrt const
> Date: Tue, 21 Jul 2009 11:52:18 +0000
>
>
>
> I try to write C code that is valid C++ and has the same meaning in C++.
>
>
> const int a = 1;
>
>
> does not have the same meaning in C and C++.
>
>
> So I would write:
>
>
> extern const int a = 1;
>
>
> which does have the same meaning in both, but for some reason gcc doesn't quite like this and gives a warning.
>
>
> I end up doing:
>
>
> /* const is extern const in C, but static const in C++,
> * but gcc gives a warning for the correct portable form "extern const" */
> #if defined(__cplusplus) || !defined(__GNUC__)
> #define EXTERN_CONST extern const
> #else
> #define EXTERN_CONST const
> #endif
>
>
> EXTERN_CONST int a = 1;
>
>
>
> which is just lame.
>
>
> - Jay