Search code examples
cgccstatic-functions

When did/does using #define to define a static function in C work?


Working in a new to me code base and I have come across some C code that looks like this:

static int* functionA(int* anInt);
#define functionA(anInt) ( <a 1 line '? :' function>)

Maybe this is obvious to people who’s C coding is a bit more fresh in their head than mine, but it looks a little odd to me. The compiler seems to agree as it spits out a message stating

error: ‘functionA’ declared ‘static’ but never defined.

(I am using gcc 4.8.2.)

As this is has turned up in some library code that we are evaluating I assume that for some compilers, some of the time, this is valid. Please could someone help explain when it is valid?

My best guess is this some old fashioned way of doing inline static function definitions. However there is every chance I am missing something subtle. An explanation of what is being achieved would also be helpful but really I want to know when this code is valid.


Solution

  • As this is has turned up in some library code that we are evaluating I assume that for some compilers, some of the time, this is valid. Please could someone help explain when it is valid?

    If there is no actual definition of functionA in the source file where it is declared it is also not used, the compiler should issue "declared ‘static’ but never defined" as a warning.

    However, if you are using the -Werror option the compiler would issue all warnings as errors... check if you are using that option.

    https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html