Can ctags be made to follow #include directives?
Asked Answered
M

1

10

I am trying to create a target in my Makefile to automatically create a tags file using ctags.

I have a list of source files (.cpp files) but I don't have a list of all the header files (I use g++ -MM to create the list of header dependencies).

I would have assumed that ctags would follow any #include directives in the .cpp files when generating the tags, but it seems my assumption is wrong.

If I create a simple tags file like this:

ctags  --fields=+iaS --extra=+q myClass.cpp

and then go into vim and type in the name of an object followed by a '.' I get the error "Pattern not found".

However, if I compile the tags file like this:

ctags  --fields=+iaS --extra=+q myClass.cpp myClass.h

and do the same thing in vim I get a lovely auto-completed list of member variables/functions.

The first line in my 'myClass.cpp' file is

#include "myClass.h"

So why doesn't ctags use that to parse the header file too?

Misquote answered 21/10, 2011 at 11:43 Comment(0)
B
7

nope. unfortunately/fortunately.

There wouldn't be too many problems following your own includes. The problem starts with

  • conditional compilation
  • external (system) libraries.

Fortunately, the problem for your own libraries is easily solved by doing something like

ctags *.h *.cpp
ctags -R src/

You could hack something together with cpp and then ctags. It would not work conveniently. A half-interesting approach would be to patch ctags to follow the #line pragma's in cpp's output and hence relate all tags to their respective sources.

However, that approach would not work as you'd expect for any preprocessor symbols/defines (macros have been expanded, so you wouldn't be able to find a macro by ctags).

The usual way to resolve this, is to generate a 'standard' tags file for your external libraries, and

:se tags+=/home/me/libs/tags.stdc++
:se tags+=/home/me/libs/tags.libsox

etc. Several libraries have tutorials on how to make a useful tags file for use with their libraries (or prebuilt tags files assuming a certain folder layout)

Bivalve answered 21/10, 2011 at 13:2 Comment(1)
Thanks for your answer. After playing around with it for a little while longer I settled on the ctags -R . option. It's a shame, I was hoping for a --follow-includes option (maybe coupled with a --include-depth=X option). But like you say, the ability to have conditional #includes makes this tricky.Misquote

© 2022 - 2024 — McMap. All rights reserved.