Show Lecture.HeaderFiles as a slide show.
CS253 Header Files
Header Files
Much like Java’s import
, C++ uses #include:
#include <foobar>
This means to compile the file foobar
(found on Linux systems
somewhere under /usr/include
), as if it were part of the source
file at this point. It typically contains declarations of
functions and classes.
It’s called a header file because you #include it at the
beginning, or head, of your .cc
file.
C Compatibility
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Hello, world!
Nudging C toward C++
However, in C++, we don’t like putting things into the global
namespace unnecessarily. This is called namespace pollution.
Or, in general:
Pure C++
Pure C++ header files, such as <iostream>, don’t use .h
.
There is no standard <iostream.h>
. Some compilers provide
it to reduce customer support calls, but it’s non-standard.
#include <iostream.h>
c.cc:1: fatal error: iostream.h: No such file or directory
compilation terminated.
Summary
This can get a bit confusing if foo starts with the letter c
.
Examples
Deprecated
- Use of the
*.h
header files is deprecated in later versions of C++.
- “Deprecated” means that they still exist, but their use is discouraged.
- Use the pure C++ versions instead.
However:
- There are still non-standard functions whose definitions live
in non-standard header files.
- Sometimes, we have to call those functions.
- For example, to call getpid(), which returns the Linux
process id, we need to
#include <unistd.h>
.
- Life is a compromise.