LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 09-06-2010, 04:10 PM   #1
nuliknol
Member
 
Registered: Jul 2009
Posts: 32

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:
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
 
Old 09-06-2010, 05:16 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
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
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 29
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
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 05:55 AM.

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