This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: strstr.c:105:3: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
- From: "Howland, Craig D. - US via newlib" <newlib at sourceware dot org>
- To: "newlib at sourceware dot org" <newlib at sourceware dot org>
- Date: Tue, 13 Aug 2019 00:05:00 +0000
- Subject: Re: strstr.c:105:3: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
- References: <CAHL7psHHH3fkr5D7R1+tap4nmKLSoirGrGpC5qoNGTZkn4G1NQ@mail.gmail.com>
- Reply-to: "Howland, Craig D. - US" <craig dot howland at caci dot com>
> From: newlib-owner@sourceware.org <newlib-owner@sourceware.org> on behalf of Giacomo Tesio <giacomo@tesio.it>
> Sent: Monday, August 12, 2019 1:10 PM
> To: newlib@sourceware.org
> Subject: strstr.c:105:3: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
>
> Looking at the build logs I've also noticed this:
>
> x86_64-jehanne-gcc
> -B/home/giacomo/projects/Jehanne/repos/jehanne-test2/hacking/cross/pkgs/newlib/build/x86_64-jehanne/newlib/
> -isystem /home/giacomo/projects/Jehanne/repos/jehanne-test2/hacking/cross/pkgs/newlib/build/x86_64-jehanne/newlib/targ-include
> -isystem /home/giacomo/projects/Jehanne/repos/jehanne-test2/hacking/cross/pkgs/newlib/src/newlib/libc/include
> -B/home/giacomo/projects/Jehanne/repos/jehanne-test2/hacking/cross/pkgs/newlib/build/x86_64-jehanne/libgloss/x86_64
> -L/home/giacomo/projects/Jehanne/repos/jehanne-test2/hacking/cross/pkgs/newlib/build/x86_64-jehanne/libgloss/libnosys
> -L/home/giacomo/projects/Jehanne/repos/jehanne-test2/hacking/cross/pkgs/newlib/src/libgloss/x86_64
> -I/home/giacomo/projects/Jehanne/repos/jehanne-test2/hacking/cross/pkgs/newlib/src/newlib/libc/sys/jehanne/include
> -DPACKAGE_NAME=\"newlib\" -DPACKAGE_TARNAME=\"newlib\"
> -DPACKAGE_VERSION=\"3.1.0\" -DPACKAGE_STRING=\"newlib\ 3.1.0\"
> -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -I.
> -Isrc/newlib/libc/string -fno-builtin -DREENTRANT_SYSCALLS_PROVIDED
> -DMALLOC_PROVIDED -DSIGNAL_PROVIDED -D_NO_GETLOGIN -D_NO_GETCWD
> -D_NO_GETPASS -D_NO_GETUT -D_NO_PWRITE -D_NO_PREAD -DHAVE_FCNTL
> -fno-omit-frame-pointer -DHAVE_INIT_FINI -g -gdwarf-2 -ggdb -O2
> -c -o lib_a-strstr.o `test -f 'strstr.c' || echo
> 'src/newlib/libc/string/'`strstr.c
> src/newlib/libc/string/strstr.c: In function ‘strstr2’:
> src/newlib/libc/string/strstr.c:105:3: error: ‘for’ loop initial
> declarations are only allowed in C99 or C11 mode
> for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
> ^
> src/newlib/libc/string/strstr.c:105:3: note: use option -std=c99,
> -std=gnu99, -std=c11 or -std=gnu11 to compile your code
> src/newlib/libc/string/strstr.c: In function ‘strstr3’:
> src/newlib/libc/string/strstr.c:115:3: error: ‘for’ loop initial
> declarations are only allowed in C99 or C11 mode
> for (int c = hs[0]; h1 != h2 && c != 0; c = *++hs)
> ^
> src/newlib/libc/string/strstr.c: In function ‘strstr4’:
> src/newlib/libc/string/strstr.c:125:3: error: ‘for’ loop initial
> declarations are only allowed in C99 or C11 mode
> for (int c = hs[0]; c != 0 && h1 != h2; c = *++hs)
> ^
> src/newlib/libc/string/strstr.c: In function ‘strstr’:
> src/newlib/libc/string/strstr.c:173:7: error: ‘for’ loop initial
> declarations are only allowed in C99 or C11 mode
> for (int i = 0; i < ne_len; i++)
> ^
> Makefile:851: recipe for target 'lib_a-strstr.o' failed
>
> I guess this use of variable initialization in the for loop works fine
> in linux, in cygwin and in amdgcn but it's a bit annoying in common
> code like strstr.c
>
> It also occurs in
>
> newlib/libc/locale/freelocale.c:49
> newlib/libc/string/memmem.c:149
>
> Is it possible to move the counter declaration outside the loop?
>
> Giacomo
A quick fix for you is to do what the error message suggests: add -std=gnu99 to CFLAGS before you configure Newlib, to have that as a default compiler flag when it is built.
This is probably also the best solution for the real fix in Newlib. There are other cases of C99 constructs being used, anyway. (It has been 20 years. Seems totally reasonable to require it.)
Craig