Why should I always enable compiler warnings?

后端 未结 20 1585
时光说笑
时光说笑 2020-11-22 00:28

I often hear that when compiling C and C++ programs I should \"always enable compiler warnings\". Why is this necessary? How do I do that?

Sometimes I also hear tha

20条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 00:47

    This is a specific answer to C, and why this is far more important to C than to anything else.

    #include 
    int main()
    {
       FILE *fp = "some string";
    }
    

    This code compiles with a warning. What are and should be errors in just about every other language on the planet (barring assembly language) are warnings in C. Warnings in C are almost always errors in disguise. Warnings should be fixed, not suppressed.

    With gcc, we do this as gcc -Wall -Werror.

    This was also the reason for the high rantyness about some MS non-secure API warnings. Most people programming C have learned the hard way to treat warnings as errors and this stuff appeared that just wasn't the same kind of thing and wanted non-portable fixes.

提交回复
热议问题