Show Lecture.HeaderFiles as a slide show.
CS253 Header Files
Header Files
Much like Java’s import
, C++ uses #include:
#include <numeric>
This means to compile the file numeric
(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++
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
.
- General rule: if the filename ends with
.h
, it puts symbols into
the global namespace. Otherwise, it puts symbols into the std
namespace.
Examples
Deprecated
- System
*.h
header files are deprecated in later versions of C++.
- Deprecated: they still exist, but their use is discouraged.
- Only system files—you can name your files whatever you like.
- Use the pure C++ versions instead.
- However, there are still valuable non-standard functions whose
definitions live in non-standard header files.
- To get the Linux process id via getpid(), we
#include <unistd.h>
.
- Do what you have to do.