什么是“私有标头”? 在C语言中?

发布于 2024-07-24 03:30:28 字数 389 浏览 4 评论 0原文

我最近一直在学习C,在我的一本教科书中,我发现了对扩展名为“.r”的文件的引用。 现在,正如您可以想象的那样,谷歌搜索“r”或“文件扩展名r”效率不高,所以我想知道您是否可以帮助我。

它出现在下面的代码块中,

#include "new.r"

static const struct Class _String = {
  sizeof(struct String),
  String_ctor, String_dtor,
  String_clone, String_differ
};

const void * String = & _String;

作者提到它是一个“私有标头”,但我希望他能更清楚地说明它到底是什么。

I've been learning C recently, and in one of my textbooks I found a reference to a file with the ".r" extension. Now, as you can imagine, googling "r" or "file extension r" is not productive, so I wonder if you could help me out.

It appears in the following code block

#include "new.r"

static const struct Class _String = {
  sizeof(struct String),
  String_ctor, String_dtor,
  String_clone, String_differ
};

const void * String = & _String;

The author mentions that it is a "private header", but I wish he could have been more clear as to what exactly that is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最佳男配角 2024-07-31 03:30:29

文件扩展名“.r”实际上没有多大意义,可能只是该作者使用的个人约定。 有些人会将他们的私有标头命名为“new_p.h”或类似的名称。

基本上,私有标头只是一个仅由特定实现文件包含的标头,而不是由消费者包含的标头。 它与语言或编译器无关,只是一种编码约定。

The file extension ".r" really doesn't mean much, probably just a personal convention used by that author. Some people would name their private header "new_p.h" or something like that.

Basically a private header is just a header that is only meant to be included by that particular implementation file, and not by a consumer. It has nothing to do with the language or the compiler it's just a coding convention.

樱花细雨 2024-07-31 03:30:29

我遇到 .r 文件的实例位于 使用 ANSI-C 进行面向对象编程,其中 .r 文件用作类的“表示”——(如果我理解正确的话)一种执行信息隐藏的方法在单独的头文件中保留内部表示并控制对类函数的访问。

只有类的实现才会引用 .r 文件,在这方面,它可以被视为类的“私有标头”。 类的外部接口,使用常规的 .h 头文件。

举例来说,一个类可能由三个文件组成:

Circle.h    /* Header file with external interfaces, such as methods. */

Circle.r    /* Representation file for internal use within the class, such as
               structs that define the internal states. */

Circle.c    /* Implementation of the Circle class. */

然后,按照惯例,使用 Circle 类的程序可能会包含 Circle.h 文件作为访问的接口班上。 Circle.r 严格由Circle 类的实现使用,而不是由其他类的实现使用,因此,使其成为“私有标头”。

r 文件扩展名基本上是一种使用的约定,而不是“官方”或一直使用的东西。 它的使用是为了方便,并与带有 h 文件扩展名的常规头文件区分开来。

The instance in which I've encountered a .r file is in Object-oriented Programming with ANSI-C, where a .r file is used as a "representation" of a class -- (if I understand correctly) a way to perform information hiding by keeping the internal representation and to control access to functions of a class in a separate header file.

Only the implementation of the class would refer to the .r file, and in that respect it could be regarded as a "private header" to the class. The interface externally to the class, a regular .h header file was used.

As an illustration, a class may be composed of three files:

Circle.h    /* Header file with external interfaces, such as methods. */

Circle.r    /* Representation file for internal use within the class, such as
               structs that define the internal states. */

Circle.c    /* Implementation of the Circle class. */

Then, by convention, a program utilizing the Circle class may include the Circle.h file as the interface to access the class. Circle.r is strictly used by the implementation of the Circle class and not by others, hence, making it a "private header".

The r file extension is basically a convention that is used, and is not something that is "official" or used all the time. It is used for convenience and to differentiate from regular header files with a h file extension.

悲喜皆因你 2024-07-31 03:30:29

C 编译器不会为文件扩展名赋予任何特定含义,因此使用 .r 扩展名只是作者按照惯例表示某些内容的方式。 由于不熟悉这本书,我不知道那可能是什么,但请放心,编译器不会为文件名附加任何特定含义。

C compilers don't attach any particular meaning to file extensions, so the use of the .r extension is just the author's way of indicating something by convention. Not being familiar with the book, I don't know what that might be, but rest assured that the compiler isn't attaching any particular meaning to the file name.

秉烛思 2024-07-31 03:30:29

据我所见过的各种开源项目所知,私有标头是仅由特定代码段(一个文件或多个文件)使用的标头。 例如,当某些文件需要访问彼此的符号/声明,但不应从其他代码访问这些符号/声明时,它们非常有用。 或者它们可用于将原型从 .c 文件中分离出来。

全局或普通标头通常存储在特殊的“include”目录中,而私有标头则保留在它们所服务的特定代码附近。

至于“dot r”扩展名,我以前从未听说过。 我见过的私有标头被命名为“dot h”,就像所有其他标头一样。

As far as I can tell from various open source projects I've seen, private headers are headers intended to be used only by a particular piece of code (a file or multiple files). They are useful when, for example, some files need access to each other's symbols/declarations, but those symbols/declarations shouldn't be accessed from other code. Or they can be used to split prototypes out of .c files.

Global or normal headers are usually stored in a special 'include' directory, while private headers stay near the specific code they serve.

As for the 'dot r' extension, I've never heard of it before. The private headers I've seen are named 'dot h', like all others.

南笙 2024-07-31 03:30:29

正如其他人所说, .r 并不重要。 私有标头可能只是意味着它不应该包含在除使用它的文件之外的任何内容中。

这段代码片段看起来像是在 C 中模拟 C++。它正在创建一个 constClass” 对象,用于保存“”的大小、构造函数、析构函数等。类”字符串。 此结构保存此 C OO 库使用的公共类信息。

As others have said, .r is not significant. The private header probably just means it's not supposed to be included by anything except the file using it.

This code snippet looks like it is sort of emulating C++ in C. It is creating a const "Class" object holding the size, constructor, destructor, etc. for the "class" String. This structure holds common class information to be used by this OO library for C.

剪不断理还乱 2024-07-31 03:30:29

我通常看到的私有标头的习惯用法是使用以下命名约定:

  • module.h - 公共接口; 客户端代码所需的名称、结构和函数。 如果可能,不要定义结构,只需声明它们的名称并在接口中使用不透明指针。 如果您提供 SDK,这将是已发布的标头。
  • modulep.h - 私有头文件; 声明及 模块内使用的定义 - 客户端代码不需要(也不应该)包含这些定义。 如果您提供 SDK,则不会发布该 SDK。
  • module.c - 实现; 这将同时使用 module.h 和 modulep.h

The idiom I've usually seen for private headers is to use the following naming convention:

  • module.h - public interface; the names, structures and functions that client code need. If possible, do not define structures, just declare their names and use opaque pointers in the interface. If you're providing an SDK, this would be the published header.
  • modulep.h - private header; declarations & definitions used within the module - client code does not need to (and should not) include these. If you're providing an SDK, this would not be published.
  • module.c - the implementation; this would use both module.h and modulep.h
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文