Creation of header files in the C language. File extension H

A similar question was recently asked to me by a colleague who is starting to program in C. And I thought that this is a good reason to share my understanding of this issue. Because even experienced programmers do not always have the same points of view on this matter.

This is partly a matter of taste, so whoever is interested in how I do it, welcome to cat.

Despite the fact that the "whole truth" about h-files is contained in the corresponding section of the gcc preprocessor description, I will allow myself some explanations and illustrations.

So, literally, a header file (h-file) is a file containing C declarations and macro definitions intended for use in several source files (c-files). Let us illustrate this.

It is easy to see that functions 1 and 2, as well as macro 2, are mentioned in both files. And since including the header files produces the same results as copying the content into each C file, we can do the following:

Thus, we simply selected the common part from the two files and placed it in the header file.
But is the header file an interface in this case?

  • If we need to use functionality that functions 1 and 2 implement elsewhere, then Yes
  • If macro 2 is intended only for use in the files Unit1.c and Unit2.c, then it has no place in the interface file
Moreover, do we really need to have two C files to implement the interface defined in the header file? Or is one enough?
The answer to this question depends on the implementation details of the interface functions and on where they are implemented. For example, if you make the diagrams more detailed, you can imagine a variant when the interface functions are implemented in different files:


This implementation leads to high code cohesion, low testability, and difficulty in reusing such modules.
In order not to have such difficulties, I always consider the C-file and the header file as one module. In which,
  • the header file contains only those declarations of functions, types, macros that are part of the interface of this module.
  • The C file, in turn, must contain the implementation of all functions declared in the h-file, as well as private types, macros and functions that are needed to implement the interface.
Thus, if I had a chance to implement the code that corresponds to the diagram above, I would try to achieve the following (the endings _с and _h in the file names are added due to the impossibility of using a period in the tool that I used to create diagrams):


The diagram shows that in fact we are dealing with two independent modules, each of which has its own interface in the form of a header file. This makes it possible to use only the interface that is really needed in this particular case. Moreover, these modules can be tested independently of each other.
The reader has probably noticed that macro 2 from the header file returned again as a copy in both C files. Of course, this is not very convenient to maintain. But making this macro part of the interface is not correct either.
In such cases, I prefer to make a separate header file containing the types and macros needed by multiple C files.

Hopefully, I managed to identify those entities that need to be placed in the header files. And also, to show the difference between interfaces and files containing declarations and macros required by several C files.

Thank you for your attention to the material.

In the C language, source files are of two types:

    header, or h-files;

    implementation files, or C files.

Header file names have a ".h" extension. Implementation file names have the extensions ".c" and ".cpp".

Header files contain only descriptions. First of all, these are function prototypes. A function prototype describes the name of the function, the return type, the number and types of its arguments. The function text itself is not contained in the h-file. Also, h-files describe the names and types of external variables, constants, new types, structures, etc. In general, h-files contain only interfaces, i.e. information necessary to use programs already written by other programmers (or by the same programmer before). Header files only provide information about other programs. When translating header files, as a rule, no objects are created. For example, in the header file you cannot define a global variable. Description line

defining an integer variable x is an error. The description should be used instead

meaning that the variable x is defined somewhere in the implementation file (which is unknown). The word extern (external) only communicates information about an external variable, but does not define that variable.

    assert.hassert is a macro that you can use to create conditions in your programs.

    ctype.h the file to include contains definitions and prototypes for routines that

classifies ASCII characters and routines that perform character conversions:

setjmp, longjmp, isalnum, isalpha, iscntrl, isprint, ispunct, isspace, toint, tolower, _tolower

    float.h

defined by executing properties of floating point numbers:

isdigit, isgraph, islower, isupper, isxdigit, toascii, toupper, _toupper

    limits.h the file to include defines explicit constants that represent

specific limits on values \u200b\u200bstored in various data types.

    math.h the include file contains prototypes and definitions for all routines for

performing mathematical calculations with floating point.

    setjmp.h the file to include defines the jmp_buf type and simulates setjmp

and longjmp subroutines.

    stdarg.h the file to include defines macros that refer to parameters in functions

with variable-length parameter lists.

    stddef.h the file to include defines offsetof a macro to which you can use

offset of structure members.

    stdio.h the file to include contains prototypes and definitions for stream I / O

subroutines. stdlib.h the file to include contains prototypes and definitions for memory allocation routines.

    string.h the file to include contains prototypes for the next line and buffer

manipulation routines.

Implementation files, or C files, contain function texts and global variable definitions. In simple terms, C files contain the programs themselves, while h files only contain information about the programs.

Presentation of source texts in the form of header files and implementation files is necessary to create large projects with a modular structure. Header files are used to transfer information between modules. Implementation files are separate modules that are developed and translated independently of each other and are combined to create an executable program.

Implementation files can include descriptions contained in header files. The header files themselves can also use other header files. The header file is included using the #include preprocessor directive. For example, descriptions of standard I / O functions are included with the line

#include

(stdio - from the words standard input / output). The h-file name is written in angle brackets if this h-file is part of the standard C library and is located in one of the system directories. The names of h-files, created by the programmer himself within the framework of the project under development and located in the current directory, are indicated in double quotes, for example,

#include "abcd.h"

Examples with. files:

    calloc.c Allocates memory for an array of elements.

    free.c Release memory allocated with calloc, malloc, or realloc.

    getkey.c Waits for a character to be received from the serial port.

    init_mem.c Initializes the memory pool used by calloc, malloc, and realloc routines.

    malloc.c Allocates a block of memory from pool memory.

    putchar.c Sends a character using the serial port.

    realloc.c Resize the pre-allocated block of memory

CARM The compiler generates many files during translation. Every file uses the same source file name ... However, each has a different extension.

    LST Files contain formatted source code along with any errors found by the compiler. The file listing can arbitrarily contain symbols used and generated by the assembly code.

    OBJ Files are object modules that contain relocatable object code. Object modules can be linked to an absolute object module.

    SRC Files are the generated translation source files of your source text.

Today, surveillance cameras can be seen at almost every turn. You may also be planning to install one or more cameras in your home. Such devices work with video files of .264 or H.264 format. But how to watch the video and how to open the file will be discussed in the article.

Differences and similarities between 264 and H.264 formats

Format. 264 Are the raw elementary streams of H.264-ES video files (also called temporary MPEG-4 video file). In turn, H.264-ES is part of the H.264 format specification. Older DVRs record video in .64 format. Such video files cannot be used for direct viewing by ordinary players and require processing by special programs.

Reduces the video to a minimum size. After the videos are fully compressed, the video and audio quality is still high. CCTV cameras and video recorders of a new type work with this format. H.264 files are also called MPEG-4 Part 10 AVC / H.264. Despite its long and scary name, it is very easy to find an H.264 file player on the net.

To open such files, you must use one of the following methods:

  • use special programs and utilities;
  • convert video files.

Opening H.264 video

Almost all popular programs and converters work with the H.264 format. Popular are:

  1. Light Alloy.

Working with format. 264

Let's take a closer look at how to open a .264 file from a DVR or surveillance camera.

Special programs

The following programs will be useful to open video.264:

.264 video files can be merged or separated. We will consider how to do this further.

Utilities

To play a .264 file, you need to place it in a container format that any media player can recognize. For this purpose, we recommend using one of the following utilities:

  1. Demuxer - can create dsm or mpc records. It is worth noting that dsm files can only be played in this utility.
  2. MKVcleaver - with its help you can cut video to .MKV.
  3. Mkvmerge - Able to modify, cut, merge or unmerge video files. After processing the video, the video quality does not deteriorate, and the format changes to .MKV.
  4. Haali Muxer - can help in converting, merging or severing video files. After video processing, it is assigned the .MKV format.
.h; sometimes C ++ header files use the extension .hpp... To avoid re-including the same code, the directives #ifndef, #define, #endif are used. In the general case, a header file can contain any constructions of a programming language, but in practice, executable code (with the exception of inline functions in C ++) is not placed in header files. For example, identifiers that must be declared in more than one file are conveniently described in a header file, and then included as needed. Modularity works the same way in most assemblers.

Traditionally, the functions of the C and C ++ standard libraries are declared in the header files.

Other languages \u200b\u200b(for example, Pascal) use an advanced module system. But in them, the header files have a certain value. The fact is that two files (main and header) are merged into one translation unit, and therefore the header file may contain preprocessor directives, incomplete syntax constructions.

Appointment

In modern programming languages, programs are composed of modules that are compiled separately. This raises the question: how to indicate that a subroutine or variable X is defined in module Y? There are several solutions for this, in C this is applied.

One of the compilation units (that is, with a -file) describes a function, for example:

Int add (int a, int b) (return a + b;)

To be able to reference it from other compilation units, you need to declare it using the function prototype, that is:

Int add (int, int); int triple (int x) (return add (x, add (x, x));)

However, such a declaration requires the programmer to provide a function declaration for add in two places - in the file containing its execution and in the file in which it is used. If the function definition changes, the programmer must remember to update all prototypes used in the program.

The header file is one solution to this problem. The module header file declares each function, object and data type that is part of call interface module - for example, in this case, the header file can only contain the declaration of the add function. Each source file referencing the add function must use the #include directive to include the header file:

/ * File triple.c * / #include "add.h" int triple (int x) (return add (x, add (x, x));)

Lists of initialized constants in the header file are selected by the preprocessor to replace them with the value of these constants in the include file. The included functions of the header file are framed with preprocessor macro protection directives to avoid duplication of them in the include file (such a situation is possible with class or file inheritance):

/ * File add.h * / #ifndef ADD_H #define ADD_H int add (int, int); #endif / * ADD_H * /

In addition to the #ifndef - #endif construction, a non-standard #pragma once is sometimes used:

/ * File add.h * / #pragma once int add (int, int);

Header files make it easier to maintain - when the definition changes, only one declaration (the one in the header file) needs to be updated. The source file can also include a header file containing the definition used in the source. This allows the compiler to check if the declaration in the h-file matches the definition in the c-file:

/ * File add.c * / #include "add.h" int add (int a, int b) (return a + b;)

Typically, header files are used only to more clearly define the interface and usually contain comments explaining how to use the components declared in the file. In the above example, the subroutines used are separated into separate source files that must be compiled separately (the exception in C and C ++ are inline functions, which are often included in the header file due to the fact that in most cases of use it is impossible to correctly expand the inline function without calls to their definition at compile time).

Comparison with directly getting headers from a compiled module

An alternative to header files is to get information about declared types, functions, etc. directly from a compiled module. This is what Pascal, Java and others do.

Benefits

The advantage of header files is primarily to simplify the compiler: without header files, the compiler and linker do the same job, checking if module Y has a compiled function X.

If the module is written correctly, conditional compilation can be used to disable some of its functionality. For example, in this case, we refuse to link the huge STL library to the program:

// unit.h #ifndef __UNIT_H__ #define __UNIT_H__ #ifndef UNIT_STL_UNUSED #include void dump (std :: ostream & os); void dump () (dump (std :: cout);) #endif void run (); #endif

// main.cpp #define UNIT_STL_UNUSED #include "unit.h" int main () (run (); return 0;)

If the module is distributed already compiled, the header file will be at the same time the documentation for using the module.

If the programmer corrected the implementation of a function in the c-file without touching the header, this will not cause a cascade recompilation of all modules that use this header.

The header file allows you to specify things that cannot be set using modules - substitutions using #define, compiler directives, incomplete syntax ...

disadvantages

The header files are much slower - to compile 10 c files, each with a long h file attached, the compiler will have to go through the header 10 times. To deal with this problem, many compilers use precompiled headers.

Header files together with some objects of the C ++ language (constants, inline-functions, templates, static-variables) form heavyweight constructions.

If the programmer suddenly changed the c-file, forgetting to do the same with the h-file, the linker will issue a vague error message without a line number. This is especially noticeable in C ++, where the same function can have a different set of arguments, and compiler-level checking does not work. If a programmer accidentally leaves a construction unfinished in an h -file, the error will be in a completely different c- or h -file.

In some languages \u200b\u200b(for example, Java), you don't need to change the code in two places at the same time.

see also

  • C Standard Library - describes the standard C header files
  • C ++ Standard Library - describes the standard C ++ header files

Links

Literature

  • Podbelsky V.V. Chapter 8. Preprocessing facilities // C ++ / rec. Dadaev Yu. G. - 4. - M .: Finance and statistics, 2003. - S. 263-280. - 560 p. - ISBN 5-279-02204-7, UDC 004.438Ci (075.8) BBK 32.973.26-018 1я173

Wikimedia Foundation. 2010.