Trevor
Consider the following minimal working example (`test.cpp`) which demonstrates my question:
```
#include <iostream>
int myvar;
int myfun(int b);
int myfun(int b){
return b*7;
}
int main(){
myvar = 8;
std::cout << myfun(3) << std::endl;
}
```
I then apply ctags as follows:
```
$ ctags -R
$ cat tag
...
main test.cpp /^int main(){$/;" f typeref:typename:int
myfun test.cpp /^int myfun(int b){$/;" f typeref:typename:int
myvar test.cpp /^int myvar;$/;" v typeref:typename:int
```
The results show that the declaration of `myvar` gets tagged, but the declaration of `myfun` does not (although the definition does).
Based on some related questions I came across on the internet, I’ve tried using ctags with the following options which did not make a difference in this case: `--language-force=C++`, `--c++-kinds=+pf`, `--extras=+q`, and `--fields=+imaSft`.
**Is there any way to tag the declaration of myvar? If not, is there another tool (e.g., cscope, YouCompleteMe for Vim) that will?**
Top Answer
Trevor
This can be done with YouCompleteMe. Here are the steps for the example described in the question (based off of the [YouCompleteMe Documentation](https://github.com/ycm-core/YouCompleteMe/tree/master#c-family-semantic-completion)):
1) install YouCompleteMe with the `--clangd-completer` or `--all` flag (note: YouCompleteMe will try to use it's own clangd executable, if you want to use a different clangd executable such as a system-wide one, then you can set the `g:ycm_clangd_binary_path` variable in your .vimrc)
2) generate a `compile_commands.json` file, which can be done by building the cpp file with CMake as follows:
a) create this basic CMakeLists.txt file:
```
cmake_minimum_required(VERSION 2.8.5)
project(Test)
add_executable(test test.cpp)
```
b) make a build directory:
```
$ mkdir build
$ cd build
```
c) do either of the following:
- use the flag `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON` in the cmake command in the next step
- add this line after the `project(Test)` line in the CMakeLists.txt file: `set(CMAKE_EXPORT_COMPILE_COMMANDS ON)`
d) run the CMake configure command:
```
$ cmake ..
```
3) copy or symlink the generated `compile_commands.json` file to the root or the project (i.e., to the directory with the cpp and CMakeLists.txt files)
4) now, if you put the cursor over over `myfun` and enter the Vim command `:YcmCompleter GoToDeclaration`, you should be taken to the declaration of `myfun`