简体   繁体   English

在C中,“公共”在放在全局变量之前意味着什么?

[英]In C, what does “public” mean when put before a global variable?

I'm going through the source code of the "less" unix tool by Mark Nudelman, and the beginning of main.c has many of the following: 我正在浏览Mark Nudelman的“less”unix工具的源代码,main.c的开头有以下几点:

public int  logfile = -1;
public int  force_logfile = FALSE;
public char *   namelogfile = NULL;

etc. in the global scope, before the definition of main(), 在全局范围内,在main()的定义之前,

What does public mean in this context? 在这种情况下公众意味着什么? And more important, where can I find this information by myself? 更重要的是,我在哪里可以自己找到这些信息? I searched using countless query combinations, and could not find this information, or any thorough C reference. 我使用无数的查询组合进行搜索,无法找到此信息或任何彻底的C引用。

In the file less.h is your answer: 在文件less.h是你的答案:

#define public      /* PUBLIC FUNCTION */

It seems like public is only used as a marker for public/global functions and variables. 公共似乎只用作公共/全局函数和变量的标记。 When compiled, it is expanded to nothing. 编译时,它会扩展为空。

How to find this information? 如何找到这些信息?

  1. Search the .c file from top to the location of the identifier you want more information about 从顶部搜索.c文件到您想要了解更多信息的标识符的位置
  2. If you do not find any declaration, look for #include directives 如果您没有找到任何声明,请查找#include指令
  3. Open any included file and look for the declaration of what you are looking for 打开任何包含的文件,查找您要查找的内容的声明
  4. Repeat from step two for every included file 对每个包含的文件重复步骤2

In this case, that was pretty simple. 在这种情况下,这很简单。

This has nothing to do with C as such. 这与C无关。 If you look in the include file less.h you will see that the author has defined a number of preprocessor instructions. 如果查看包含文件less.h,您将看到作者已定义了许多预处理器指令。 Some of them like 'public' is most likely for readability. 其中一些像“公共”的人最有可能获得可读性。 Eg: 例如:

 /*
 * Language details.
 */
#if HAVE_VOID
#define VOID_POINTER    void *
#else
#define VOID_POINTER    char *
#define void  int
#endif
#if HAVE_CONST
#define constant    const
#else
#define constant
#endif

#define public      /* PUBLIC FUNCTION */

See how public is defined. 了解公众的定义。 It's translated to nothing and as you have already figured out it's in the global scope. 它被翻译成没有,因为你已经发现它在全球范围内。 However it's more readable and more obious that it's in the global scope. 然而,它在全球范围内更具可读性和更直观性。 Also, one could argue that if the source is written consistently like this and a new version of C emerges that does have a public keyword, it's a matter of redefining the the header file and recompile to actually use it. 此外,有人可能会争辩说,如果源是这样编写的,并且新版本的C出现并且有一个公共关键字,则需要重新定义头文件并重新编译以实际使用它。

Preprocessing tricks like this can even be used in clever ways to have one source compile in different languages (like C++ and Java). 像这样的预处理技巧甚至可以巧妙地用于让一个源编译成不同语言(如C ++和Java)。 This is not something you should be doing, but it's possible to it. 这不是你应该做的事情,但它是可能的。

The options like HAVE_VOID you see in the example from less.h above are usually specified as compiler (actually preprocessor) options on compile time. 您在上面的less.h示例中看到的HAVE_VOID等选项通常在编译时指定为编译器(实际上是预处理器)选项。 So if you have a compiler and a version of C that supports the void keyword you would compile your source with: 因此,如果您有一个支持void关键字的编译器和C版本,那么您将使用以下命令编译源代码:

g++ -g -DHAVE_VOID -Wall myprog.C -o myprog g ++ -g -DHAVE_VOID -Wall myprog.C -o myprog

Everywhere the author uses VOID_POINTER in the source would then actually be considered by the compiler as: 作者在源中使用VOID_POINTER的任何地方实际上都会被编译器视为:

void * 

If you didn't specify HAVE_VOID the compiler would instead use 如果您没有指定HAVE_VOID,编译器将改为使用

char * 

which is a reasonable substitue. 这是一个合理的替代品。

TIP: Check your compiler's options to see if you have an option to just preprocess your sources. 提示:检查编译器的选项,看看是否可以选择预处理源。 That way you can look at the actual source that gets sent to the compiler. 这样,您可以查看发送到编译器的实际源。

The definition of public as an empty pre-processor macro has been addressed in other answers. 公共作为空预处理器宏的定义已在其他答案中得到解决。 To find the definition, you probably want to use a tool like ctags/etags or cscope. 要查找定义,您可能希望使用ctags / etags或cscope等工具。 (There are many tools to scan a source tree to generate this information.) For example, you can find the definition of public at line 55 of less.h by invoking: (有许多工具可以扫描源树以生成此信息。)例如,您可以通过调用以下命令找到less.h第55行的public定义:

$ ctags -dtw *.c *.h
$ vi -t public

Or, simply run ctags before you start editing anything. 或者,在开始编辑任何内容之前,只需运行ctags。 When you see a definition you don't understand, put the cursor on it and type ^] (that's control-right square bracket, and will work in vi-like editors.) 当你看到一个你不理解的定义时,把光标放在它上面并输入^](这是控制右方括号,并且可以在类似vi的编辑器中工作。)

C没有关键字“public”,因此它可能是在较少的源代码中定义的宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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