c - Two functions with same name: One static How to declare in header file -
c - Two functions with same name: One static How to declare in header file -
i have 2 .c files names: main.c , test2.c. defined function: void testextern2()
in main.c. defined function same name static: static void testextern2()
in test2.c. set declaration: void testextern2();
in header file.
i compile:
$ gcc main.c test2.c , error: error: static declaration of 'testextern2' follows non-static declaration
is there way around this. mean want provide function declaration in header file need maintain name of both functions same.
the same function cannot declared both static , non-static in same translation unit. if test2.c
genuinely needs static function named testextern2
must not include header file conflicting declaration of function.
that doesn't prevent declaring (non-static) function in header file; means source file containing static version must not include header, or else header must allow utilize conditional compilation suppress non-static version of declaration necessary. example:
testextern.h
#ifndef testextern_h #define testextern_h #ifndef suppress_testextern2 int testextern2(int); #endif #endif
test2.c
#define suppress_testextern2 #include "testextern.h" static double testextern2(const char *s) { /* ... */ }
do note, however, "testextern2" exceedingly unusual name static
function, because static linkage , external linkage mutually exclusive. understand, too, 2 distinct functions having same name not in confer advantage on whatsoever. despite sharing name, different functions, , in given translation unit, every appearance of name refer same 1 of them. can lead confusion, however, because name refer different functions in different translation units.
it much improve give different functions different names, @ to the lowest degree 1 has external linkage.
c header-files
Comments
Post a Comment