Skip to content

Instantly share code, notes, and snippets.

@xuhdev
Last active June 13, 2024 15:47
Show Gist options
  • Save xuhdev/1729992 to your computer and use it in GitHub Desktop.
Save xuhdev/1729992 to your computer and use it in GitHub Desktop.
Generate ctags file for C or C++ files and its depedencies (included header files). This could avoid you to always generate a huge tags file.
#!/bin/sh
# https://www.topbug.net/blog/2012/03/17/generate-ctags-files-for-c-slash-c-plus-plus-source-files-and-all-of-their-included-header-files/
# ./ctags_with_dep.sh file1.c file2.c ... to generate a tags file for these files.
gcc -M "$@" | sed -e 's/[\\ ]/\n/g' | \
sed -e '/^$/d' -e '/\.o:[ \t]*$/d' | \
ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q
@ohhmm
Copy link

ohhmm commented Jun 23, 2017

@candh
Copy link

candh commented Apr 12, 2019

This won't work on MacOS since sed on MacOS does not interpret '\n' as a newline.
So, another way of doing this would be:

gcc -M $* | tr '\\ ' '\n' | sed -e '/^$/d' -e '/\.o:[ \t]*$/d' | \
        ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q

you could also replace the sed with gsed, and another hacks. but the above works for me. Thanks for the idea!

@Wurstnase
Copy link

I like it.
Add this directly into my makefile.

	@$(CC) -M $(CFLAGS) $(SRC) | sed -e 's/[\\ ]/\n/g' | \
		sed -e '/^$$/d' -e '/\.o:[ \t]*$$/d' | \
		ctags -L - --c-kinds=+p --fields=+iaSl --extra=+q --langmap=c:.c.h

@mehrshad-kh
Copy link

This won't work on MacOS since sed on MacOS does not interpret '\n' as a newline. So, another way of doing this would be:

gcc -M $* | tr '\\ ' '\n' | sed -e '/^$/d' -e '/\.o:[ \t]*$/d' | \
        ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q

you could also replace the sed with gsed, and another hacks. but the above works for me. Thanks for the idea!

Well, it works just fine for me as of February 2023 on an Intel Mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment