Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

How to find location of C header file errno.h on macOS?

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)
question from:https://stackoverflow.com/questions/65866733/how-to-find-location-of-c-header-file-errno-h-on-macos

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

This is cumbersome, and a lot of packages with their own build systems won't be aware of this requirement. So the useful option is to set a shell variable in your ~/.bashrc - shell init files are a mess on OSX!

export CPATH=`xcrun --show-sdk-path`/usr/include/

Or whatever syntax your shell favours. I recommend the above, as 'sh' (typically 'bash') is fundamental, and other shells inherit the variable. (Though since Catalina, you should probably be focusing on zsh init files, since it's replacing bash ...)

$CPATH will be honoured by both clang and gcc - I'm not sure it's technically a POSIX / BSD / ISO C standard (probably outside the scope of the latter); but is effectively ubiquitous across platforms.

So now you can just use: less $CPATH/errno.h, or the preprocessor as:

clang -E $CPATH/errno.h

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...