make编译源码时报error: ‘for’ loop initial declarations are only allowed in C99 mode的解决办法

  1. 现象描述
    今天在编译tree-2.0.2的源码时报错如下:
    tree.c:668:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
    for(int i=0; i < ipattern; i++)
    ^
    tree.c:668:3: note: use option -std=c99 or -std=gnu99 to compile your code
[root@k8s-master tree-2.0.2]# make
gcc -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o tree.o tree.c
In file included from tree.c:20:0:
tree.h:63:1: warning: C++ style comments are not allowed in ISO C90 [enabled by default]
 // Start using PATH_MAX instead of the magic number 4096 everywhere.
 ^
tree.h:63:1: warning: (this will be reported only once per input file) [enabled by default]
tree.c:47:1: warning: C++ style comments are not allowed in ISO C90 [enabled by default]
 //off_t (*listdir)(char *, int *, int *, u_long, dev_t) = unix_listdir;
 ^
tree.c:47:1: warning: (this will be reported only once per input file) [enabled by default]
tree.c: In function ‘main’:
tree.c:100:3: warning: ISO C90 forbids mixed declarations and code [-Wpedantic]
   bool needfulltree;
   ^
tree.c:124:29: warning: ISO C90 forbids compound literals [-Wpedantic]
   lc = (struct listingcalls){
                             ^
tree.c:138:3: warning: ISO C90 forbids mixed declarations and code [-Wpedantic]
   char *stddata_fd = getenv(ENV_STDDATA_FD);
   ^
tree.c:145:33: warning: ISO C90 forbids compound literals [-Wpedantic]
       lc = (struct listingcalls){
                                 ^
tree.c:263:30: warning: ISO C90 forbids compound literals [-Wpedantic]
    lc = (struct listingcalls){
                              ^
tree.c:271:30: warning: ISO C90 forbids compound literals [-Wpedantic]
    lc = (struct listingcalls){
                              ^
tree.c:279:30: warning: ISO C90 forbids compound literals [-Wpedantic]
    lc = (struct listingcalls){
                              ^
tree.c: In function ‘usage’:
tree.c:659:2: warning: string length ‘3348’ is greater than the length ‘509’ ISO C90 compilers are required to support [-Woverlength-strings]
  "  --            Options processing terminator.\n");
  ^
tree.c: In function ‘patignore’:
tree.c:668:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
   for(int i=0; i < ipattern; i++)
   ^
tree.c:668:3: note: use option -std=c99 or -std=gnu99 to compile your code
tree.c: In function ‘patinclude’:
tree.c:679:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
   for(int i=0; i < pattern; i++)
   ^
tree.c: In function ‘getinfo’:
tree.c:712:3: warning: ISO C90 forbids mixed declarations and code [-Wpedantic]
   int isdir = (st.st_mode & S_IFMT) == S_IFDIR;
   ^
tree.c: In function ‘do_date’:
tree.c:1279:7: warning: ISO C90 does not support the ‘%e’ gnu_strftime format [-Wformat=]
       strftime(buf,255,"%b %e  %Y",tm);
       ^
tree.c:1281:7: warning: ISO C90 does not support the ‘%e’ gnu_strftime format [-Wformat=]
       strftime(buf,255,"%b %e %R", tm);
       ^
tree.c:1281:7: warning: ISO C90 does not support the ‘%R’ gnu_strftime format [-Wformat=]
tree.c: In function ‘printit’:
tree.c:1304:2: warning: ISO C90 does not support the ‘%lc’ gnu_printf format [-Wformat=]
  if (iswprint(*tp)) fprintf(outfile,"%lc",(wint_t)*tp);
  ^
tree.c: In function ‘psize’:
tree.c:1351:59: warning: ISO C90 does not support ‘long long’ [-Wlong-long]
   } else return sprintf(buf, sizeof(off_t) == sizeof(long long)? " %11lld" : " %9lld", (long long int)size);
                                                           ^
tree.c:1351:94: warning: ISO C90 does not support ‘long long’ [-Wlong-long]
   } else return sprintf(buf, sizeof(off_t) == sizeof(long long)? " %11lld" : " %9lld", (long long int)size);
                                                                                              ^
tree.c:1351:3: warning: ISO C90 does not support the ‘ll’ gnu_printf length modifier [-Wformat=]
   } else return sprintf(buf, sizeof(off_t) == sizeof(long long)? " %11lld" : " %9lld", (long long int)size);
   ^
tree.c: In function ‘fillinfo’:
tree.c:1398:50: warning: ISO C90 does not support ‘long long’ [-Wlong-long]
   if (inodeflag) n += sprintf(buf," %7lld",(long long)ent->linode);
                                                  ^
tree.c:1398:3: warning: ISO C90 does not support the ‘ll’ gnu_printf length modifier [-Wformat=]
   if (inodeflag) n += sprintf(buf," %7lld",(long long)ent->linode);
   ^
make: *** [tree.o] Error 1

  1. 原因分析
    gcc默认采用的C89编译器,C89不支持在for循环中初始化增量。
  2. 解决方案
    将Makefile文件中的CC=gcc改为CC=gcc -std=c99,重新make即可。
[root@k8s-master tree-2.0.2]# make
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o tree.o tree.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o list.o list.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o hash.o hash.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o color.o color.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o file.o file.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o filter.o filter.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o info.o info.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o unix.o unix.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o xml.o xml.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o json.o json.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o html.o html.c
gcc -std=c99 -O3 -pedantic -Wall -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o strverscmp.o strverscmp.c
gcc -std=c99 -s -o tree tree.o list.o hash.o color.o file.o filter.o info.o unix.o xml.o json.o html.o strverscmp.o
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值