LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   make gcc to report prototype errors (https://www.linuxquestions.org/questions/programming-9/make-gcc-to-report-prototype-errors-830656/)

nuliknol 09-06-2010 04:10 PM

make gcc to report prototype errors
 
Hi, I have one simple question: how to tell gcc compiler to check for external functions to be matched by its prototype?

Consider this example:
Code:

master tests # cat file1.c
#include <stdio.h>
#include "file1.h"
void func1(int a) {
    printf("a=%d\n",a);
}
master tests # cat file2.c
main() {
    float f;
    func1(f);
    func1();
}
master tests #

I am making 3 mistakes here:
  1. I don't include header declaration in file2.c
  2. I am calling func1() with float paramter
  3. I am calling func1() without any parameter
And GCC allows this to compile without any warning:

Code:

master tests # gcc -c file1.c
master tests # gcc -c file2.c
master tests # gcc -o program file1.o file2.o
master tests #

Now we have a buggy program that will produce unpredicted results:

Code:

master tests # ./program
a=1
a=-1
master tests #

My code has so many files that tracking where did I include the prototype or where i missed it becomes a big problem.
I have been searching on how to make gcc to warn about this:
Code:

master tests # gcc -c -Wmissing-prototypes -Wmissing-declarations -Wbad-function-cast file1.c
master tests # gcc -c -Wmissing-prototypes -Wmissing-declarations -Wbad-function-cast file2.c
master tests # gcc -o program -Wmissing-prototypes -Wmissing-declarations -Wbad-function-cast file1.o file2.o
master tests #

but I had no luck.

Can someone tell me what warnings or flag will prevent these examples from sucessful compilation?
GCC version: gcc version 4.4.3 (Gentoo 4.4.3-r2 p1.2)

Thanks in advance

JohnGraham 09-06-2010 05:16 PM

In file2.c, -Wmissing-prototypes/-Wmissing-declarations would only have warned you if you'd tried to define func1 without a declaration/prototyped declaration. Since you're only using it, these won't produce a diagnostic.

The specific warning I think you want is -Wstrict-prototypes, but -Wall includes this and is a good catch-all, too.

ArthurSittler 09-06-2010 05:30 PM

put function protypes in .h file
 
nuliknol,

You can write a global .h file, which prototypes all the global functions and types in the project. I do not include global variables in this .h file because I avoid using global variables.

I write a .h file for each .c file. The header file declares function prototypes for every function exported by that .c file. Then I make a global .h file which includes the .h files for all of the constituent files. I include the global header file in all of the .c files. That tells all the .c files the function name, parameter list, and return type for every function implemented in the .c files.

You could go one step further and make two .h files for every .c file. One .h file prototypes all the functions you want to make visible to all the files. The other .h file prototypes the functions that you wish to keep local. Declaring all the local functions as static functions prevents those functions from being linked to other files. This also provides a method for providing publicly accessible functions in the API for the software while restricting access to the internal functions. This can be used if you are forced to withhold source code and provide software in compiled form only.

There are many variations on this theme. The point is that all the files need to see the same prototype for every function that is accessible by more than one of the files.

I typically assign a constant with a name derived from each .h file. I use the constant as a switch so that the .h file is included once and only once. For a .c file cfile1.c, I would have a corresponding cfile1.h. I name the inclusion switch CFILE1_H and write the cfile1.h as follows:
Code:

#ifndef CFILE1_H

    /* typedefs for structs and prototypes of functions */
    ...
    ...
    ...

    #define CFILE1_H
#endif  // end of ifndef CFILE1_H
// end-of-file of cfile1.h immediately follows the #endif directive



All times are GMT -5. The time now is 06:11 AM.