Don't see find all references option when using Visual Code for a C project

Viewed 10171

Trying out Microsoft Visual Code, not seeing find all references in right click menu? What am I missing?

4 Answers

You need to install C/C++ extension for VSC that you can find here or at the VSC marketplace

In summary, you need to install the C/C++ Intellisense with the help of GNU Global tags extension and GNU Global and run gtags on your workspace folder.

Most people would have the extension installed, but not GNU Global, which you can check by doing which global to see the path to the binary or global --version to check that the version is 6.5 or higher, as indicated in the README of the extension.

Building and installing "global" from source is pretty straight-forward if you've done this sort of thing before, but in the spirit of showing how to plant an apple tree instead of giving an apple, here's how I went about installing gnu global on my Ubuntu 19.04 machine. Hopefully this can be easily adapted to other Linux environments.

1) Download the latest version

wget http://tamacom.com/global/global-6.6.3.tar.gz

2) Extract files and go into its folder

tar -xzvf global-6.6.3.tar.gz
cd global-6.6.3

3) Prepare for build

./configure

4) Check if dependencies are required and install them

I ran into this error:

configure: checking "location of ncurses.h file"... configure: error: curses library is required but not found. If you are not going to use gtags-cscope, please try ./configure --disable-gtagscscope

To find out how to get this missing file, we can use apt-file. Install it with:

sudo apt install apt-file

It should tell you that "The system-wide cache is empty. You may want to run 'apt-file update'". Follow the advice:

sudo apt-file update

Now we can do:

apt-file find ncurses.h

Which shows:

ekg2-core: /usr/share/doc/ekg2-core/book-en/plugin-ncurses.html
ekg2-core: /usr/share/doc/ekg2-core/book-pl/plugin-ncurses.html
latrace: /etc/latrace.d/headers/ncurses.h
libghc-ncurses-doc: /usr/lib/ghc-doc/haddock/ncurses-0.2.16/ncurses.haddock
libncurses-dev: /usr/include/ncurses.h
libncurses-dev: /usr/include/ncursesw/ncurses.h

The "/usr/include/" folder is a good hint that "libncurses-dev" is what we need. By the way, when installing packages for a build, the one with the "dev" suffix is usually the right one:

sudo apt install libncurses-dev

This is an iterative process. Go back to step 3 until ./configure returns no more errors. Luckily for me, this was it.

5) Build global

This is just the usual make. I ran into another error:

WARNING: 'makeinfo' is missing on your system.

Which was fixed with:

sudo apt install texinfo

Ran make again and all was good.

6) Install

You'll need to run this as root so it can install to directories owned by root, such as "/usr/local/bin". These are likely already on your PATH environment variable, which means that we just need to do:

sudo make install

7) Create tag files

Let's check that we now have it correctly installed:

global --version

Which shows

global (GNU GLOBAL) 6.6.3 Powered by Berkeley DB 1.85. Copyright (c) 1996-2018 Tama Communications Corporation License GPLv3+: GNU GPL version 3 or later http://www.gnu.org/licenses/gpl.html This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

Now go to your workspace folder and do:

gtags

It won't tell you anything, so if you get no errors, this is it. Now you can go back to VS Code and try "Find all references" again. It should just work!

Incidentally, NetBeans 8.2 used to have a really good "find references" feature and even a cool call graph, if you get the C/C++ bundle, and it all works out of the box. This is now gone in more recent versions since Apache took over the project, but the good news is that VS Code is pretty good once this installation is done. And the VS Code integration with gdb rocks!

STATUS today:

  • C/C++ extenstion for VSC still doesn't support this. The latest "insider" version 0.26 is searching - very very slow - and not finding all references.

  • GNU Global can't understand C++ correctly (search for all references to MyClass::open() finds all "open" functions in the code - not only belonging to this class)

Working solution for now: CCLS see https://github.com/MaskRay/ccls/wiki/Visual-Studio-Code

Related