[net.lang.c] Undefined static function

guy@sun.uucp (Guy Harris) (06/23/85)

> Every declaration which is outside the definition of any function
> declares an extern static something, even with "extern" or "static"
> or both omitted.

False.  See 11.2 "Scope of externals":

	Identifiers declared "static" at the top level in external
	definitions are not visible in other files.

Any declaration outside the definition of any function with "static" omitted
declares an "extern" something, which is static in the sense of not being
"auto" but not static in the sense of being "static"; it's visible in other
files.

	Guy Harris

fdc@cstvax.UUCP (Frank Cringle) (06/24/85)

Here's a question for all the C language lawyers out there.  What should
a C compiler make of the following:

	static char *tgoto();

	main()
	{
		.....
		if (tgoto(......).....
		.....
	}

In other words, we declare a function to be static, use it, but do not
define it in the source file.  This occurs in libcurses/cr_tty.c (4.2BSD).
The 4.2 compiler happily accepts this and links the 'static' tgoto to a
definition in another source file.  In this case it is what the author
intended, but it should really have been thrown out by the compiler,
methinks.
-- 
Frank Cringle, Dept. of Computer Science, Univ. of Edinburgh
UUCP:  <UK>!ukc!{hwcs,kcl-cs,ucl-cs,edcaad}!cstvax!fdc
JANET: fdc@UK.AC.ed.cstvax  or   fdc@UK.AC.ed.ecsvax

adam@npois.UUCP (Adam V. Reed) (06/27/85)

Every declaration which is outside the definition of any function
declares an extern static something, even with "extern" or "static"
or both omitted.
				Adam Reed
				ihnp4!npois!adam

beal@polaris.UUCP (John L. Beal) (09/18/85)

> Here's a question for all the C language lawyers out there.  What should
> a C compiler make of the following:
> 
> 	static char *tgoto();
> 
> 	main()
> 	{
> 		.....
> 		if (tgoto(......).....
> 		.....
> 	}
> 
> In other words, we declare a function to be static, use it, but do not
> define it in the source file.  This occurs in libcurses/cr_tty.c (4.2BSD).
> The 4.2 compiler happily accepts this and links the 'static' tgoto to a
> definition in another source file.  In this case it is what the author
> intended, but it should really have been thrown out by the compiler,
> methinks.
> -- 
> Frank Cringle, Dept. of Computer Science, Univ. of Edinburgh
> UUCP:  <UK>!ukc!{hwcs,kcl-cs,ucl-cs,edcaad}!cstvax!fdc
> JANET: fdc@UK.AC.ed.cstvax  or   fdc@UK.AC.ed.ecsvax

From postnews Wed Sep 18 13:49:21 1985
> 
> 	static char *tgoto();
> 
> 	main()
> 	{
> 		.....
> 		if (tgoto(......).....
> 		.....
> 	}
> 
> In other words, we declare a function to be static, use it, but do not
> define it in the source file.  This occurs in libcurses/cr_tty.c (4.2BSD).
> The 4.2 compiler happily accepts this and links the 'static' tgoto to a
> definition in another source file.  In this case it is what the author
> intended, but it should really have been thrown out by the compiler,
> methinks.
> -- 
> Frank Cringle, Dept. of Computer Science, Univ. of Edinburgh
> UUCP:  <UK>!ukc!{hwcs,kcl-cs,ucl-cs,edcaad}!cstvax!fdc
> JANET: fdc@UK.AC.ed.cstvax  or   fdc@UK.AC.ed.ecsvax

Who knows. C knows that functions are extern (K&R says that somewhere)
so that a i
char f();

will be extern. Maybe it just ignores that static in some implementations
and believes it in others?

beal@polaris.UUCP (John L. Beal) (09/18/85)

> Every declaration which is outside the definition of any function
> declares an extern static something, even with "extern" or "static"
> or both omitted.
> 				Adam Reed
> 				ihnp4!npois!adam

Not true. Static is used to hide the declaration from other files
and the default of automatic to scope it throughout the program.

g-chapma@gumby.UUCP (09/20/85)

Regarding the "undefined static function" problem/question, viz:

	static char * f();

	main ()
	  {
	    ...
	    if (f(x)) ...
	    ...
	  }

	[end of file, no function definition for f]

Some compilers allow you to put the definition of statics (functions or
variables) in another file, as long as you combine the file containing 
the definitions with the file containing the references at some point 
before linkage time.

Whitesmiths' C compiler, for example, produces assembly code.  You must
compile each C source file separately (giving separate files of assembly 
code), but if you assemble the outputs together, the assembler will 
resolve the references.  The linker then sees no global symbol associated 
with the statics.

I took advantage of this feature to circumvent an inconvenient limit 
imposed by this compiler on the size of string constants.  I wrote a C 
source preprocessor which replaced specially-marked string constants with 
references to static string variables, and coded those constants in 
assembly code in a separate file.  I modified the compiler shell script
to run the preprocessor before the compiler, and assemble the string 
constant file along with the compiler output.

--Ralph Chapman
  g-chapma @ wisc-gumby @ uwvax