11

C is ubiquitous, and C++ nearly as much, but I'm curious which other languages follow a similar model of header files. Specifically, in which other languages is it idiomatic to edit pairs or groups of "header" and "implementation" files? (A group as in multiple headers with one corresponding implementation file or a single header with multiple implementation files, as also seen in C.) For example, the pair "blah.h" and "blah.c".

C's header files are tied into the C preprocessor, but that's not the detail I'm focusing on here. For example, PHP has various include mechanisms, but you don't have a pair/group of "blah-header.php" plus "blah-notheader.php".

Some languages generate a file from source in a one-to-one mapping, such as Java, but that's not what I'm talking about either. I'm interested in cases where the programmer directly edits both/all files in the pair/group.

Perhaps to put it another way: which languages have a declaration file ("header") and definition file ("implementation") such that a person would generally edit both files in tandem?

7 Answers7

9

Ada

Any Ada package on the other hand consists of two parts, the specification (header) and body (code). The specification however is a completely stand alone entity which can be compiled on its own and so must include specifications from other packages to do so. An Ada package body at compile time must refer to its package specification to ensure legal declarations, but in many Ada environments it would look up a compiled version of the specification.

--file example.ads, the package specification.
package example is
:
:
end example;

--file example.adb, the package body. package body example is : : end example;

Source: http://www.adahome.com/ammo/cpp2ada.html#2

4

Objective-C (and and its C++ counterpart, Objective-C++) can #include C (C or C++) header files as well as #import Objective-C header files. That pairs are even used is by convention.

In additon, ASP, Perl, Python, Pascal, and, since the file extension used for its includes doesn't have to follow a standard, PHP.

Edit: Most assembler source code also use "paired" included/implementation files by convention.

EDIT2 - ADDENDUM Apparently there's some confusion as to the use cases that prompted the use of separate header files in C, and so some developers are dubious that certain other languages would also use header files to deal with those use cases in the same way.

  • as the earliest C compilers ran on machines with a small amount of memory, it wasn't possible to load up symbol tables with all the source that would be incorporated into the final build. So each source file was run through multiple passes separately to generate the object code with necessary interfaces for other sources included in headers;
  • as the earliest C compilers ran on machines with a small amount of memory, it was more efficient to distribute the shared elements of a module among more specialized headers which could be included selectively by sources rather than in one;
  • as the earliest C compilers ran on slow machines, especially since they had a small amount of memory, it was time-consuming to compile lots of source files belonging to a module when only a few had been changed;
  • sometimes source code maintainers don't share their code with outside groups, but instead make the object code available with headers of the required interfaces to aid in proper integration with linked code;
  • and most importantly these days, programs and the language runtime sometimes deal with libraries, especially platform-dependent libraries, which are from an external environment or other language but which they can integrate with appropriate interface specifications in a header.

The last point is particularly true when a language supports a platform's GUI. I know it's been true on the Mac since its earliest days (when Pascal was its first high-level language) to now for Perl (now automated), Python, and PHP. Its runtime environment may not make the inclusion process visible to you, but it's happening; especially if you use platform-specific symbols.

Creating bindings for C libraries in Free Pascal

h2xs builds a Perl extension from C header files, or use SWIG, or support C++

Interfacing Python to a C lib doesn't require just a header, but writing an extension!

Huperniketes
  • 2,225
3

I think Modula 2 uses a similar pattern

Tim
  • 956
3

F#: It optionally allows a .fsi file to declare the symbols like C headers. See http://en.wikibooks.org/wiki/F_Sharp_Programming/Modules_and_Namespaces#Module_Signatures

Ming-Tang
  • 856
2

You can do this in D with .di files. It's useful for distributing closed source libraries and such. The .di files can also be generated automatically from the .d files. However, unless you need to distribute a closed-source library or compile times become a major issue and you need to cut down on what the compiler has to parse, it's not idiomatic.

dsimcha
  • 17,284
0

I've seen the C Preprossessor used in Haskell to include code from a header. Its not that common as far as I know though.

alternative
  • 1,522
0

Erlang has header files that are often used (among other things) to declare data structures used in a module. They are not necessary, though, and many, if not most, modules don't use them.

mipadi
  • 7,533