C Programming/Headers
Lesson objectives:
[edit | edit source]- Learn about C header files and their usage.
- Learn about some common errors to avoid.
Lesson
[edit | edit source]What is a C header file?:
[edit | edit source]A C header file is a file containing C source code that is to be included into a file or files for compilation. Generally the reason for doing this is to inform the compiler about available functions to use and variables that are accessible, which otherwise could not be known about until link time, causing errors. Of course, it is possible to just write all the code in one file, but this can easily obfuscate code, make finding the correct code harder, and stops relinking with minimal recompilation, because you have to recompile everything whenever you make a change, instead of recompiling one file and then relinking. Note that after altering a header file, it can be necessary to recompile ALL files that include that header file. C header files should have a .h extension, however C++ header files may also use this extension - though now it is more common to use the .hpp extension (if you use .cpp for source files) or occasionally .h++ (for some rare .c++ extensions) for these C++ files. The system already provides a set of header files for programmers, which allow access to the C Standard Library functions, macros and typedefs.
Common errors:
[edit | edit source]Since header files by nature are included multiple times, it is important to avoid redefinition errors. If you use static functions or variables, then due to the nature of these variables, everything is fine. However, the definition of such functions/declaration of such variables will raise an error in other cases. The exception to this is by declaring a variable without allocating memory. The keyword that does this is extern - it tells the compiler that somewhere (with visible scope) there is a variable with such a name. Note the function declarations (which are implicitly extern due to RAM allocation - more at the end), are fine. Normally, a header file will contain extern declarations, and then the actual functions and variables will be defined in separate C file, which then gets linked into the final executable, eg.
//In file header.h: int function(int, int); //Declaration of function - tells compiler a function will be defined in the final executable. extern int a; //Tells compiler an int a will be defined somewhere in the final executable (with global scope - again explained later on page.
//In file header_code.c: int a; int function(int x, int y) { return (20 * x) & y | 0x0efa68; //Random thing to do. }
//In file main.c: #include <stdio.h> #include "header.h" int main(void) { printf("%d", function(316, 472); return 0; }
You can compile exactly this code with gcc -o program.o header_code.c main.c while you are in the directory with these files.
Extra:
[edit | edit source]The reason you do not have to declare a function as extern is because the compiler already knows that with a declaration it must be. The reason is, all functions have global scope, as they are non-modifiable - they are stored, in other words, in a separate memory segment for executable code. Memory is not (and cannot) be allocated when a function is declared. With a variable, however, due to scope, a variable may only be made available during certain times of program execution, so a variable may have memory allocated before that memory gets assigned a value. Due to debugging frames of memory and other debugging information, along with extern actually being available, using a token (a name which identifies a variable or function, normally used to mean - in this context - the name inside the source code and/or compiled code (in a debugging frame)) multiple times is not an option (without static). Basically, at a variable declaration, memory is allocated, whereas with a function declaration, memory is not, so the compiler needs to worry about treating the syntax correctly, not you.