1

I am working on getting FreeGLUT to build on OSX and am running into many instances of the same problem. Many of the functions exist only in the .c files.

Here's an example

extern void fghRestoreState( void );

static void fghRestoreState( void ){...}

I have a limited understanding of C, but the compiler errors seem to make sense:

src/Common/freeglut_gamemode.c:252: error: static declaration of ‘fghRestoreState’ follows non-static declaration
src/Common/freeglut_gamemode.c:43: error: previous declaration of ‘fghRestoreState’ was here

My question is, is there any reason they set it up this way? Would it compile on other platforms correctly?

Kyle
  • 1,978
  • 1
  • 18
  • 30

1 Answers1

6

The keyword extern in front of the function means external linkage.
It enables you to use functions defined in other Translation units to your own source file.
In Simple words, It enables you to use the fghRestoreState() in another file which does not include the declaration of it.

Whereas, the keyword static implies an Internal Linkage, that is the function should be visible only in the file in which is it defined and declared.
In Simple words, it tells the compiler I will be using this function only in this source file so hide it from all the other files in my project.

The error since as stated above there is a conflict in using the two keywords together.
You cannot tell the compiler to enable all files to see this function(using extern) & again tell it, hide it from all other files(using static).

So choose the keyword as per your usage of the function.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • That was my understanding, and so I couldn't understand why the library would have that code in the first place. Is there any condition where this would make sense? So far as I could tell there were no ifdefs or anything that would cause this to make sense. – Kyle Jan 21 '12 at 06:47
  • @Kyle: Given my answer which your compiler verify's,I can't think of a reason,Why someone would do that. – Alok Save Jan 21 '12 at 07:38