Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix duplicate cscope ? is it a better way?

Tags:

vim

ctags

cscope

It is several years I am programming with vim and I used ctags. I am working with a reasonably large C/C++ package and I need to find definition of functions. I usually use grep + ctags. Recently I tried to use cscope instead of ctags and installed it with Vundle. I see the following error for some of my files

E568: duplicate cscope database not added

I searched the web and found this: https://blogs.oracle.com/natarajan/entry/avoiding_duplicate_cscope_database_error It doesn't work.

How can I fix this?

like image 606
Aznaveh Avatar asked Jan 11 '17 21:01

Aznaveh


3 Answers

Expanding on Artem's answer:

The Vim help for cscopeverbose is as follows:

If 'cscopeverbose' is not set (the default), messages will not be printed indicating success or failure when adding a cscope database. Ideally, you should reset this option in your .vimrc before adding any cscope databases, and after adding them, set it. From then on, when you add more databases within Vim, you will get a (hopefully) useful message should the database fail to be added.

The problem here is that (a) there are multiple scripts attempting to load the cscope.out file and (b) they're not following the best practices of disabling the "verbose" cscope warnings before loading the file then re-enabling it afterwards, as suggested by the help text above.

The full error output should tell you which script is triggering this warning; for me it looked like this:

Error detected while processing /home/me_and/.vim/plugin/cscope_maps.vim:
line   42:
E568: duplicate cscope database not added

The fix was then to edit the ~/.vim/plugin/cscope_maps.vim file to add set nocscopeverbose immediately before the cs add ... lines. My version of this file already had set cscopeverbose immediately after, but if yours doesn't you should add that too.

like image 97
me_and Avatar answered Nov 17 '22 01:11

me_and


Found the solution which worked for me (here: http://thoughtsolo.blogspot.com/2014/02/cscope-issue-duplicate-cscope-database.html):

Just add this line "set nocscopeverbose " to your ~/.vimrc file.

like image 7
Artem Avatar answered Nov 17 '22 02:11

Artem


As per the blog, "This error pops up when VIM is already compiled with 'CSCOPE' module and you have also installed "cscopemenu.vim"". I assume that you have a vim executable with has been configured with --enable-cscope option.

Here's what I do:

  • Download cscope source and build it, install the executable in a directory which is available in your PATH
  • Download vim source code and configure it with --enable-cscope, build the source and install the executable
  • Download cscope_maps.vim and place it under $HOME/.vim/plugin directory. This contains cscope settings for vim.
  • Create cscope database out of the source and header files. You may do something like the following

find $PROJECT_HOME -name *.c -o -name "*.cpp" -o -name "*.cc" -o -name "*.h" -o -name "*.hpp" > cscope.files

cscope -qbR -i cscope.files

You can add these commands in an alias and excute the alias every time you want to update your cscope database. These two commands create finally create cscope.out database file.

  • Update .vimrc file to have the following

    if has("cscope") set csprg=<location to cscope executable> set csto=0 cs add <location to cscope.out> endif

I hope after doing these steps you should be able to use cscope with vim easily.

Note that if you are working on multiple projects, you should be able to add appropriate environment variables to enable vim to pick the correct cscope database.

To answer your second question, may I suggest using tagbar. This will list your function names in the current source or header file. You can install it using Vundle. Add the following line to your .vimrc

Plugin 'majutsushi/tagbar'

Add this to your .vimrc to toggle tagbar view

nmap <F4> :TagbarToggle<CR>

Note that F4 is just an example and you may use any binding to do the same.

like image 2
Rishi Avatar answered Nov 17 '22 03:11

Rishi