Reputation: 833
I start to work on a huge project with tones of C and C++ files, already wrote by someone else.
Is there any faster/simpler ways to find in what file any macro or function is define other than a grep -r
? It is kind of long.
In some IDE there is this magical thing like right click and "go to definition". But I'm currently using emacs. I don't know if there is any customisation that can do this ?
Each time, I have to copy past the name in my terminal, run a grep
and re copy past the file path in my emacs. (And you know, I am lazy...)
Upvotes: 2
Views: 593
Reputation: 41648
Each time, I have to copy past the name in my terminal, run a
grep
and re copy past the file path in my emacs.
You can improve on this by using M-x rgrep
inside Emacs. It asks for the regular expression, a glob pattern of files to look in, and a directory to start in. It then does a recursive grep, outputs the results in a buffer, and you can jump directly from the hits to the corresponding file.
For the glob pattern, you could type something like *.c
, or you could use one of the aliases defined in the variable grep-files-aliases
. For example, ch
is equivalent to *.[ch]
(C source and header files) and cchh
is equivalent to *.cc *.[ch]xx *.[ch]pp *.[CHh] *.CC *.HH *.[ch]++
(C++ source and header files).
You might find that this works well enough that you don't need ctags and other tools suggested in the other answers and comments.
For ease of finding function definitions in C, some projects use the convention that the function name in the definition starts in the first column:
/* this is just a declaration */
int main(int argc, char *argv[]);
/* in the definition, the function name starts on its own line */
int
main(int argc, char *argv[])
{
...
That means that you can find the definition, excluding any calls to the function, with the regex ^main
.
Upvotes: 2
Reputation: 877
CTags. You can try using Ctags with emacs and it will help you to navigate to the function declaration directly. For its usage, please refer to https://www.emacswiki.org/emacs/EmacsTags
You can also explore Cscope, It has a better feature set than ctags which works directly on pattern recognition. But sometimes, you just need to navigate through code and more often than not ctags does the job.
Upvotes: 4
Reputation: 30155
grep
should be really fast if you limit the search to the directories and file types (generally .h
and .hpp
) that are likely to contain it. For example if you know it is in your application just search there, if you know it's from FreeType (generally FT_*
) search there.
More RAM will help the system cache files better, and if your on a HDD best to get an SSD. If your working directly on a VM, especially one with remote disks, if can work locally that will often be faster.
Otherwise many fully functional IDE's (Visual Studio, XCode, Eclipse, etc.) have C++ integration to keep track of these things, and will for example offer a "Go to declaration" and "Go to definition" option as a shortcut or context menu when over a symbol.
Upvotes: 1