简体   繁体   中英

extern function prototype and static definition

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM