Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-06-2010, 04:10 PM   #1
Registered: Jul 2009
Posts: 42

Rep: Reputation: 0
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:
master tests # cat file1.c
#include <stdio.h>
#include "file1.h"
void func1(int a) {
master tests # cat file2.c
main() {
    float f;
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:

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:

master tests # ./program
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:
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
Old 09-06-2010, 05:16 PM   #2
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
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.

Last edited by JohnGraham; 09-06-2010 at 05:18 PM.
Old 09-06-2010, 05:30 PM   #3
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 30
put function protypes in .h file


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:
#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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LFS 6.3 .... 6.12.1. Installation of GCC make errors Axo Linux From Scratch 4 03-29-2008 02:18 AM
Chapter 6.12 GCC make -k check errors kz_chopper Linux From Scratch 2 02-20-2007 11:54 PM
gcc compiling errors when i do make install TheBrick Linux - Software 2 03-09-2006 10:29 AM
LFS Chap 5 - gcc first make command errors antken Linux From Scratch 3 03-17-2003 10:38 PM
make and gcc compile errors sedandawk Linux - General 1 12-18-2002 02:56 AM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration