gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Information on GCC warnings

#31916 - abilyk - Wed Dec 15, 2004 10:42 pm

Hey guys,

Today I started compiling with -Wall and am working on cleaning up all the warnings that show up now (and here I thought my code was all nice and shiny!). Does anyone know of any resources for info on GCC warnings? I have to admit I don't really know what a lot of these warnings actually mean (like near initialization), and I've had little luck searching on Google. I've been able to get rid of a lot through trial and error, but I'd like to make this a more educated process if possible.

Thanks,
Andrew

#31922 - abilyk - Wed Dec 15, 2004 11:45 pm

I'm still interested in finding some resources just for learning's sake, but I've already cleaned up almost every error. The one that remains I could easily fix, but I'd have to break the standards I've been coding under and I'd prefer to avoid that. The way I've been doing it, every .c file (barring main.c) has a corresponding header file. All functions are declared in the headers and defined in the .c files. Here are the details:

In mathematics.h:
Code:
CODE_IN_IWRAM        void SeedMTRand(u32 s);
CODE_IN_IWRAM        u32  MTRand(void);
CODE_IN_IWRAM static void NextState(void);


In mathematics.c:
Code:
CODE_IN_IWRAM void SeedMTRand(u32 s)
{
    ...
}

CODE_IN_IWRAM u32 MTRand(void)
{
    ...
    NextState();
    ...
}

CODE_IN_IWRAM static void NextState(void)
{
    ...
}


As it is here, I get a warning in the .h file, `NextState' declared `static' but never defined. The header file can't see the definition because the definition is static, so I understand why I'm getting the error. If I remove the static from the header, I get a warning in the .c file, static declaration for `NextState' follows non-static. This sounds misleading to me at least, because it's the static definition that's following the non-static declaration. But regardless, my declaration apparently needs to be "static" as well.

Simple (and perhaps the only) solutions would be to declare the function in my .c file instead of the header file or to completely forgo a declaration and move the function definition above the MTRand function (which references NextState). However, as mentioned above I'd like to keep things consistent, so if there's some sort of loophole to allow me to keep the declaration in the header, I'd love to hear it.

Thanks,
Andrew

#31928 - poslundc - Thu Dec 16, 2004 12:05 am

abilyk wrote:
Simple (and perhaps the only) solutions would be to declare the function in my .c file instead of the header file


This is the correct solution.

Header files are for the public information that tells another module how to interface with your module. Static functions are, by definition, hidden from other modules and therefore should not appear in header files.

From a pragmatic standpoint, if I have a static function f in foo.c and put its prototype in foo.h, then #include this header into bar.c, I've just prototyped a function in bar.c that is inaccessible to it, which is erroneous.

In sum, declare your prototypes in two places: public access routines in the header file, local routines in the C file. This public/private-classification coding practice applies to things like structures and macros as well.

Dan.