Skip to content

Instantly share code, notes, and snippets.

@orikad
Last active July 7, 2021 08:19
Show Gist options
  • Select an option

  • Save orikad/603ea97e33bb9e5ce07f55a36d53bdce to your computer and use it in GitHub Desktop.

Select an option

Save orikad/603ea97e33bb9e5ce07f55a36d53bdce to your computer and use it in GitHub Desktop.
Get header paths from the compiler
#!/usr/bin/env bash
if [ -z $1 ]
then
echo "Usage: $0 path/to/system/header.h"
echo "Example: $0 fcntl.h"
echo "Opens header in \$EDITOR ($EDITOR)"
exit -1
fi
if [ -z $EDITOR ]
then
echo "Warning: \$EDITOR is not set. Fallbacking to \`vim\`."
EDITOR=vim
fi
CC="gcc"
set -o pipefail
fname=$(basename "$1" | sed s/\\./\\\\./)
path=$(echo "#include <$1>" | $CC -x c - -M | grep -o "/.*$fname")
if [ ! -z "$path" ]
then
echo "$path"
IFS=$'\n'
for l in $path
do
$EDITOR "$l"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment