views:

619

answers:

8

Hi

I've been learning c recently, and in one of my textbooks I found a reference to a file with the extension .r (dot r). 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...

Bonus points if you also recognize what book this code snippet is from! As always, thanks for your help!

z.

+4  A: 

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.

Greg Hewgill
Actually, GCC uses the file extension to figure out how to handle the file - it feeds .c files to the preprocessor/compiler/linker, .cpp files to the C++ preprocessor/C compiler/linker, .o files to the linker only, etc. Of course this can be overridden by command-line options, and .r isn't one of the recognized extensions (as far as I know), so I'm not saying you're wrong, but I figured it was worth mentioning the behavior.
David Zaslavsky
@David: That's true for files specified on the command line, but not for files #included through the preprocessor.
Greg Hewgill
+11  A: 

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.

Gerald
If it was me, I would probably lean towards one header, foo.h for foo.c, with #ifdef blocks in it to disable the stuff I did not want other files to have (and possibly an #ifndef or two for a few "extern" lines in front of a variable or two?). - - - - - Perhaps the Author uses a convention where *.h is "published"/"released", but *.r is not to protect "intellectual property" or some such rot???
Roboprog
"NewP.h" is also somewhat popular, it's used for instance in X11, and "new_p.h" is used for instance in Qt.
ninjalj
+2  A: 

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.

Eduard - Gabriel Munteanu
A: 

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.

Zifre
+1  A: 

No idea about the book, but a query on google code search led me to this file.

fretje
damn cant get that query right... bug with markdown maybe?
fretje
That's it, that's the very code indeed!
Ziggy
+4  A: 

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.

coobird
You got the reference! Object-Oriented Programming, staring ANSI-C! There is cake for you, you just have to know where to look.
Ziggy
A: 

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
Michael Burr
A: 

Cool, I came from my google query about what the .r file meant in the same book!! I didn't know what the .r meant either, I thought it first it could of been some weird mistake but he used it again and so here I am. So glad Ziggy posted this a year ago, wasn't sure I'd get an answer from ".r private header"....Thanks :)

OOC_Reader
It's a pretty great book! I'm glad to hear someone else is enjoying it!
Ziggy