5 ms·
semi related, but I'm coming back to C++ after a long hiatus (15 years). I realize this is probably a newb question... The code base I'm working in is very lar
by bradford 5y ago
semi related, but I'm coming back to C++ after a long hiatus (15 years). I realize this is probably a newb question...
The code base I'm working in is very large and I have a recurring problem where I see a term (class/variable/etc) being used in a cpp file, and want to know which header file contains the definition.
What's the quickest, easiest way to do this?
I've been using grep, but the size of the code base, combined with the large number of #includes in each cpp file, makes this inefficient.
I believe I can use ctags/vim, but I last used that circa 2000 and I'm curious to know what other static analysis solutions have cropped up since then.
Does IWYU address this scenario? I'm using clang as a compiler if that's at all relevant.
- anand-bala 5y agoIn most cases, what you are looking for is a language server like `clangd` (works for most compilers) [1]. You can find a Language Server Protocol implementation for your editor at [2] (I don't think it lists __all__ clients, but it should include the most popular ones). EDIT: I realized that this is a vague answer, so let me clarify. An LSP implementation (especially clangd) provides actions like `go-to definition` or `find references` that you would find in full-featured IDEs like CLion (which is also amazing BTW). Since you mentioned vim, I am guessing you use it and don't necessarily want to let go of the hand-crafted vimrc you have created. Adding an LSP plugin to Vim is incredibly easy and gives you these "IDE" features with customizable mappings. [1]: https://clangd.llvm.org/ https://clangd.llvm.org/ [2]: https://langserver.org/#implementations-client https://langserver.org/#implementations-client
- bradford 5y agoThanks! I read about using LSP/Clangd with vim via [coc](https://github.com/clangd/coc-clangd https://github.com/clangd/coc-clangd) and I think that's the path I'll try going down. Other responses, thanks for your input. Just want to clarify that I have tried VS and VSCode with limited success (sometimes search works, sometimes it doesn't, and my biggest gripe is an occasional lack of transparency into what's going on under the cover). I think any solution is going to require some investment on my part and LSP sounds like a good investment.
- blcArmadillo 5y agoThere are lots of options: - Yes, ctags/vim would work - You could use something like vscode - Consider checking out cscope. With cscope you can also build a reverse index which lets you find where things are called. It can be used with something like vim but also has a pretty nice TUI.
- drummer 5y agoIf you use Visual Studio, it is as easy as right clicking on the typename or variable and choosing to go to the declaration or definition.
- inetknght 5y ago> The code base I'm working in is very large and I have a recurring problem where I see a term (class/variable/etc) being used in a cpp file, and want to know which header file contains the definition. A good IDE will have a feature to let you locate the declaration and/or definition of any variable or type. I've found that a lot of IDEs have that feature completely broken. Qt Creator, for example, is easily confused and comes with all kinds of Qt garbage^H^H^H^H^H^H^H^H baggage. CLion is a resource hog and often just hangs. Visual Studio is usually pretty good -- assuming you're using Windows. VS _Code_ is "okay" but I've found it's more of a headache to set up. I don't have experience with XCode since I've never used OSX for development. I've found the most reliable way is to learn how to use `grep` and pair that with understanding where to search; the project source directory of course but also system headers and any libraries installed to non-system locations. That knowledge translates to usefulness in other workflows too.
- f00zz 5y agoI use ctags+vim every day with rather large C++ codebases (but then again I'm a dinosaur).
- bradford 5y agoCurious, I was under the impression that ctags offers a 'jump to definition' functionality, but little more. (i.e., 'find all references' isn't supported). Is that correct? Do you use if for functionality beyond the 'jump to definition/jump back to previous context'?
- f00zz 5y agoThat's correct, there's "jump to definition" but no "find all references". I've used cscope for a while for that, but never really got used to it (and it doesn't work that well with C++). I really should get with the times and try LSP like others suggested.
- jcelerier 5y agoI use Qt creator and in that IDE it'd just be "F2" on the symbol (it uses clang behind the scenes so it's semantically accurate)