Version française

gccheaders

An interesting Zsh function, which can also be used as a script: gccheaders. In summary, here's what it does:

if [[ $# -eq 0 ]] then
  echo "Usage: gccheaders header_file | gcc_option ..." >&2
  return 1
fi
set -x
printf '#include <%s>\n' "${@:#-*}" | \
  "${CC:-gcc}" -H -E "${(M)@:#-*}" - 2>&1 >/dev/null

The "${@:#-*}" allows the arguments that do not start with a dash to be selected: these are headers to include. Thus the command printf '#include <%s>\n' "${@:#-*}" generates a small part of C code, which will be preprocessed (thanks to the option -E) by gcc (or another command given by the environment variable CC, which can be another version of gcc). The option -H allows the names of the included files to be output to the standard error stream, which is redirected to the standard output with 2>&1; the normal output is discarded with >/dev/null. The "${(M)@:#-*}" allows the arguments starting with a dash to be selected; they are used as other gcc options.

Here is an example of how this Zsh function can be used:

$ gccheaders -std=c99 limits.h stdint.h
+gccheaders:13> printf '#include <%s>\n' limits.h stdint.h
+gccheaders:14> gcc -H -E '-std=c99' -
. /usr/lib/gcc/x86_64-linux-gnu/4.2.3/include/limits.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.2.3/include/syslimits.h
... /usr/lib/gcc/x86_64-linux-gnu/4.2.3/include/limits.h
.... /usr/include/limits.h
..... /usr/include/features.h
...... /usr/include/sys/cdefs.h
....... /usr/include/bits/wordsize.h
...... /usr/include/gnu/stubs.h
....... /usr/include/bits/wordsize.h
....... /usr/include/gnu/stubs-64.h
. /usr/include/stdint.h
.. /usr/include/bits/wchar.h
.. /usr/include/bits/wordsize.h


webmaster@vinc17.org