Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

gcc - Catching Missing Semicolons in C header file prototypes

This topic ought to have been flogged to death. I just spent 30 minutes locating what ended up being a missing semicolon at the end of a function prototype in a header file:

void foo(void);
void bar(void)  // <<< Error on this line
void squee(void);

This is a common typo caused by copy-pasting the prototype from the C file. Of course according to the compiler the universe just fell apart, with an endless stream of absolutely nonsensical errors, none of them helpful.

This could be avoided by having an optional parsing phase to check for this condition in .h files then report a warning (promoted to error if settings mandate). This would require some restrictions on what you put in header files (no code, consistent format for prototypes, etc). But that's an easy compromise.

I can write my own SW tool to do this, but it would be more helpful to run it as a part of the build process. I use GCC in Eclipse. Any advice on where you'd start with this? Or anything pre-existing / off the shelf? Or perhaps just a better way to approach it?

Thank you.

question from:https://stackoverflow.com/questions/65947859/catching-missing-semicolons-in-c-header-file-prototypes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

it's far more common and more difficult to guess the following problem (in a header file):

struct something {
   type1 var1;
   type2 var2;
}
/* EOF */

and when you #include "header.h" into hello.c

#include <stdio.h>
#include "header.h"

int main(int argc, char **argv)
{
        printf("Hello, world
");
}

you get an error e.g.

lcu@europa:~$ gcc main.c
main.c:4:1: error: expected ';', identifier or '(' before 'int'
    4 | int main(int argc, char **argv)
      | ^~~

and the compiler has got out of header.h to signal the error in the line of main function. The thing can be worse if you happen to use legacy code and declare main() the old way:

main(argc, argv)
char **argv;
{
...

because then, the struct is a valid type and it is taken as the type returned by main() and you get (if you get it) the error far below (or no error at all, just a warning):

lcu@europa:~$ gcc main.c
main.c: In function 'main':
main.c:4:1: warning: type of 'argc' defaults to 'int' [-Wimplicit-int]
    4 | main(argc, argv)
      | ^~~~

In this case, the contents of main.c were:

#include <stdio.h>
#include "header.h"

main(argc, argv)
char **argv;
{
        printf("Hello, world
");
}

which is still valid c code.

(these examples were made by gcc, because clang ---the native compiler of freebsd--- detects the EOF in the header file and shows a warning stating that the type was not ended before the end of the include file. But this only happens if the type definition is the last of the file.

Note:

if you declare main as:

#include <stdio.h>
#include "header.h"

main(argc, argv)
int argc;
char **argv;
{
        printf("Hello, world
");
}

you get a complete compilation, without even a warning.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...