This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
extern const initialized warns in C
- From: Jay K <jayk123 at hotmail dot com>
- To: gcc <gcc at gcc dot gnu dot org>
- Date: Sun, 21 Jan 2018 07:12:09 +0000
- Subject: extern const initialized warns in C
- Authentication-results: sourceware.org; auth=none
- References: <MWHPR18MB121493D9F2292547E0AF6C3AE6ED0@MWHPR18MB1214.namprd18.prod.outlook.com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
extern const int foo = 123;
Why does this warn?
This is a valid portable form, with the same meaning
across all compilers, and, importantly, portably
to C and C++.
I explicitly do not want to say:
const int foo = 123
because I want the code to be valid and have the same meaning
in C and C++ (modulo name mangling).
I end up with:
// Workaround gcc warning.
#ifdef __cplusplus
#define EXTERN_CONST extern const
#else
#define EXTERN_CONST const
#endif
EXTERN_CONST int foo = 123;
and having to explain it to people.
$ cat 1.c
extern const int foo = 123;
$ $HOME/gcc720/bin/gcc -c -S 1.c
1.c:1:18: warning: 'foo' initialized and declared 'extern'
extern const int foo = 123;
^~~
$ $HOME/gcc720/bin/gcc -c -S -xc++ -Wall -pedantic 1$ $HOME/gcc720/bin/gcc -v
Using built-in specs.
COLLECT_GCC=/Users/jay/gcc720/bin/gcc
COLLECT_LTO_WRAPPER=/Users/jay/gcc720/libexec/gcc/x86_64-apple-darwin16.7.0/7.2.0/lto-wrapper
Target: x86_64-apple-darwin16.7.0
Configured with: ../gcc-7.2.0/configure -prefix=/Users/jay/gcc720 -disable-nls -disable-bootstrap
Thread model: posix
gcc version 7.2.0 (GCC) $
Thank you,
- Jay