[转载] How to resolve “cc1: all warnings being treated as errors” ?

How to resolve “cc1: all warnings being treated as errors” ?

C ProgramsCompilationErrors & FailuresProgramming Languages

If you are compiling some C program or open source package using Makefile which is written by someone else, there are chances you may encounter error as “cc1: all warnings being treated as errors” and you will not be able to compile the program or package.

So this error, tells us that there are some “warnings” in the program and those are getting considered as “errors” forcing you to fist fix those errors to proceed with the compilation, so there are following ways to proceed,

1. Its always better to resolve those errors if you have time.
2. If you dont have time, and can ignore “warnings” for a while, just search the code as,

 grep -r Werror * 

to identify which Makefile is enabling “Werror”
3. remove/delete this completely from that Makefile or Change it to “-wno-error”
4. save this makefile and recompile the code. This will show the warnings but will not stop compilation reporting those as errors.

Now, let me show how this works with the simple program.

Create a helloworld.c as below,

1

2

3

4

5

6

7

8

#include <stdio.h>

 

int main(int argc, char **argv) {

        void *p;

        printf("Hello World\n");

        p = 10;

        return 0;

}

Above program purposefully creates a warning in which we are trying to set integer number to void pointer.
Now, lets try to compile this program as it is,

$ gcc helloworld.c
helloworld.c: In function ‘main’:
helloworld.c:6:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  p = 10;
    ^

This actually went ahead to create a.out by showing one warning as above. But in case of big programs, we dont know how many actual warnings will be,
hence we will enable “show all warnings” macro “-Wall” during command line,

 $ gcc helloworld.c -Wall
helloworld.c: In function ‘main’:
helloworld.c:6:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  p = 10;
    ^
helloworld.c:4:8: warning: variable ‘p’ set but not used [-Wunused-but-set-variable]
  void *p;
        ^

Now, lets force all those warnings as errors, so someone should not be able to create executable without resolving this errors,

$ gcc helloworld.c -Wall -Werror
helloworld.c: In function ‘main’:
helloworld.c:6:4: error: assignment makes pointer from integer without a cast [-Werror=int-conversion]
  p = 10;
    ^
helloworld.c:4:8: error: variable ‘p’ set but not used [-Werror=unused-but-set-variable]
  void *p;
        ^
cc1: all warnings being treated as errors

And now the altimate workaround to temprarily proceed with creating executable without resolving these error, if you just need to get it compiled somehow since your boss has asked to get it done asap. 

$ gcc -o helloworld helloworld.c -Wall -Wno-error 
helloworld.c: In function ‘main’:
helloworld.c:6:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  p = 10;
    ^
helloworld.c:4:8: warning: variable ‘p’ set but not used [-Wunused-but-set-variable]
  void *p;
        ^

And this is how it will create the executable without resolving this warnings.

$ ls
helloworld  helloworld.c

Same things can be done in Makefiles by passing CFLAGS=-Wno-error & CFLAGS=-Werror

Tags: cflagscompilationCompilation Errorerrorsgccmakefilewarnings

POSTS YOU MAY ALSO LIKE...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值