4

I am new to C language and am trying to view the source for the header file errno.h.

How can I:

  1. Figure out where the header file is stored on my computer?
  2. View the source of the header file?

What I've Tried

From this answer, running gcc --print-file-name=errno.h in my $HOME directory just outputs errno.h.

From this answer, running cpp -dM /usr/include/errno.h | grep 'define E' | sort -n -k 3 outputs:

clang: error: no such file or directory: '/usr/include/errno.h'
clang: error: no input files

From this answer, running clang -E /usr/include/errno.h outputs:

clang: error: no such file or directory: '/usr/include/errno.h'
clang: error: no input files

One solution I know works is running sudo find / -name "errno*.h" -type f. However, this returns many results. I am wondering if there's some programmatic way to find the source using a C-related tool (e.g. by invoking gcc).

My Computer

  • macOS version: 10.15.7 (Catalina)
  • clang --version --> Apple clang version 12.0.0 (clang-1200.0.32.27)
Brett Hale
  • 21,653
  • 2
  • 61
  • 90
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119

1 Answers1

8

It's not entirely clear what you're trying to do here - since <errno.h> is a standard library header, you will find it in: /usr/include/errno.h ... Edit - I can see your issue with Catalina now. See below:

At least on my (older) OSX box, this header isn't very informative - including <sys/errno.h> in turn, which does at least provide the symbolic constants: EPERM, ENOENT, ... (see: man intro for an overview)

As <sys/errno.h> itself includes further system headers, albeit headers that rarely concern user-space development, you can get an overview of how the compiler recursively finds these headers using the preprocessing stage:

clang -E /usr/include/errno.h - this works for gcc too.


For Catalina, the headers are now located under the SDK. I'm sure there are reasons for this - multiple SDKs (e.g., iphone development, etc), and some post-hoc rationale for security preventing the creation of a /usr/include directory. In any case, for Catalina, you need the Xcode tool: xcrun (see: man xcrun) to find the SDK path to: .../usr/include

`xcrun --show-sdk-path`/usr/include

provides the path, so to view <errno.h> :

less `xcrun --show-sdk-path`/usr/include/errno.h

and consequently, you can run the preprocessor with:

clang -E `xcrun --show-sdk-path`/usr/include/errno.h
Brett Hale
  • 21,653
  • 2
  • 61
  • 90