Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@osirisgothra

This comment has been minimized.

Copy link

commented Jun 3, 2015

awesome

@dragonly

This comment has been minimized.

Copy link

commented Jul 23, 2016

非常棒,不过还要手动去Makefile里面找IncludeDir之类的,虽然还好

@squm

This comment has been minimized.

Copy link

commented Jan 2, 2017

tr '\\ ' '\n'

@ohhmm

This comment has been minimized.

Copy link

commented Jun 23, 2017

why have not gcc an option to generate tags file?

@ohhmm

This comment has been minimized.

Copy link

commented Jun 23, 2017

@candh

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Apr 28, 2019

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.