Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make gcc warn about unknown functions?

Tags:

c

linux

gcc

Consider this code:

int function()
{
  int a = 1 ;
  int b = helper(&a);
  return b ;
}

int main()
{
  function();
  return 0 ;
}

This code snippet compiles to object code without problem using gcc, despite the fact that the function called 'helper' has not been declared. I know that the linker should catch this but I've seen obscure bugs which resolved once the correct headers (containing the function declarations) were included, despite the linker and compiler not generating any errors.

There are a number of gcc warnings which seem to be related but do not actually achieve what I want: -Wmissing-prototypes, -Wmissing-declarations and -Wstrict-prototypes. Unfortunately, these warnings are limited to missing prototypes when global functions are defined, I'm interested in warnings about missing prototypes when global functions are referenced.

Can anyone suggest alternatives?, thanks.

like image 583
Gearoid Murphy Avatar asked Jul 30 '12 16:07

Gearoid Murphy


People also ask

How do I enable warnings in GCC?

You can request many specific warnings with options beginning with ' -W ', for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning ' -Wno- ' to turn off warnings; for example, -Wno-implicit .

How do I make GCC warnings as errors?

You can use the -Werror compiler flag to turn all or some warnings into errors.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

What is I flag in GCC?

Please point me to a source or explain in brief, What does the Flag -I does? It instructs the compiler to add the argument of the flag -I to the include files search path. In Unix/Linux world you typically start from man gcc when need to find about comand line flags of gcc .


2 Answers

You want -Wimplicit-function-declaration warning.

Personally, I prefer compiling my code with -Wall -Wextra.

like image 52
Maxim Egorushkin Avatar answered Sep 20 '22 20:09

Maxim Egorushkin


Use -Wl,--no-undefined to ensure a forced undefined function error.

like image 41
Elda Peller Avatar answered Sep 19 '22 20:09

Elda Peller