Search code examples
cgccc99

GCC C99 Disable compilation of main() without return


How to force gcc compilator throw error when int main() have no return statement. This code compiles without any errors

#include<stdio.h>

int main(){
   printf("Hi");
}

I am using

gcc  -Wall -Wextra -std=c99 -Wreturn-type -Werror -pedantic-errors a.c

command for compilation


Solution

  • You can avoid the special treatment that main receives by renaming it. Compiling with -Dmain=AlternateName -Werror=return-type yields “error: control reaches end of non-void function”.

    Naturally, you would do this as a special compilation to test for the issue and not use the object module resulting from this compilation. A second normal compilation without -Dmain=AlternateName would be used to generate the object module.