开发者

Meaning of public modificator for function in C

PUBLIC void main

This is from kernel.c from Minix source.开发者_如何学JAVA What is the meaning of PUBLIC in this case?


It's probably defined like this:

#define PUBLIC extern
#define PRIVATE static

Ooops, just looked in my copy of Tanenbaum. It is defined as:

#define PUBLIC

i.e. as nothing. It's simply a bit of "self-documentation". PRIVATE is defined as I originally said. You can find these in the Minix source file const.h.


Quoted from Tanenbaum The Minix Book Operating Systems Design and Implementation Third Edition Page 140 paragraph 3

PRIVATE is defined as a synonym for static. Procedures and data that are not referenced outside the file in which they are declared are always declared as PRIVATE to prevent their names from being visible outside the file in which they are declared. As a general rule, all variables and procedures should be declared with local scope, if possible. PUBLIC is defined as the null string. An example from kernel/proc.c may help make this clear. The declaration

PUBLIC void lock_dequeue(rp)

comes out of the C preprocessor as

void lock_dequeue(rp)

static global variables have file scope. So if you define a global variable or make a function static, then those will be visible only within that file. That is you will only be able to access those within the code which is within that file, in a multi-file environment.

extern globals are visible/accessible from outside the file. For function definitions extern is optional as they are visible from outside the file's scope by default.

Hash defining these things to PRIVATE and PUBLIC are nothing but adding a layer of abstraction for better interpretation and understanding of what actually is intended. As in OOP design the private and public has the interpretations, adding the same names indicate what properties do they have.


I guess it will be replaced by preprocessor to some more C like (or to empty string).

Try and search after #define with PUBLIC


The word PUBLIC implies that it marks the method as part of the "public interface" of the compilation unit, that is, it's a method which can be called from outside the current source file. (This is just an educated guess). As others have pointed out, PUBLIC is almost certainly a preprocessor macro which expands to some set of appropriate keywords/attributes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜