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.
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.
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.
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.
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.
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.
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