Header files names in C, C++, and POSIX
A student in CPSC 351 asks:
I’m trying to use the
<errno>
header file, and I get the following error:$ c++ test.cpp test.cpp:3:10: fatal error: errno: No such file or directory 3 | #include <errno> | ^~~~~~~ compilation terminated. $
The short answer
Use this instead:
#include <cerrno>
The long, complicated answer
If you’re programming in C, or including a C header file in a C++ program, you need to include the file extension .h
. So you’ll see in the documentation for errno(3)
that you need to do:
#include <errno.h>
The C++ convention is to not include the .h
extension. But since it’s a C header file,
#include <errno>
won’t work.
You could add the .h
, but you shouldn’t. errno
is part of the C standard library (which you can tell because it’s in section 3 of the manual). Since C++ was originally based on C, there are versions of all the C standard library header files included in the C++ standard library, but the convention in that library is to drop the .h
suffix and prefix the filename with the letter c
to show that it came from the C standard library. So you should use:
#include <cerrno>
to get errno
. You can see this convention at work in the sample code, where I wrote:
#include <cstdlib>
to bring in the constants EXIT_SUCCESS
and EXIT_FAILURE
. If I were programming in C I’d do this instead:
#include <stdlib.h>
What about header files from other libraries?
Given what I’ve just said, you may be wondering why the sample code has lines like this:
#include <unistd.h>
instead of something like this:
#include <cunistd>
That’s because <unistd.h>
isn’t part of the C or C++ standard library. It’s part of the POSIX API, which includes many (but not all) Linux system calls. So it’s not in either the C or C++ standard libraries, and since UNIX programmers tend to use C, there’s only a single version of the header, and it ends in .h
.