Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let cscope use absolute path in cscope.out file?

Tags:

vim

cscope

Everytime after load a cscope.out in Vim, I need to change Vim's "pwd" to the same directory as cscope.out file is under, which might be due to that cscope use relative path when generating tag file. So if there is a way to force cscope to use absolute path in its tag file - cscope.out, then it will be regardless of whether the pwd of your Vim session is the same as the directory that cscope.out file is under.

like image 894
Haiyuan Zhang Avatar asked Feb 02 '10 22:02

Haiyuan Zhang


3 Answers

You can ask vim to interpret the paths in cscope.out relative to the location of the cscope.out file by setting the cscoperelative option. From :help csre:

If 'cscoperelative' is set, then in absence of a prefix given to cscope (prefix is the argument of -P option of cscope), basename of cscope.out location (usually the project root directory) will be used as the prefix to construct an absolute path. The default is off. Note: This option is only effective when cscope (cscopeprg) is initialized without a prefix path (-P).

Examples

: set csre
: set nocsre

like image 113
Neha Karanjkar Avatar answered Oct 18 '22 10:10

Neha Karanjkar


When importing cscope.out, you can supply the prefix, i.e.

:cscope add /path/to/cscope.out /path/to/src/code

Then your searches will turn up like:

Cscope Tag: foobar
    #   line  filename / context / line
    1     21 /path/to/src/code/foobar_file.c
like image 12
Aaron H. Avatar answered Oct 18 '22 10:10

Aaron H.


The cscope tutorial has a very simple workaround for this problem:

11.Try setting the $CSCOPE_DB environment variable to point to a Cscope database you create, so you won't always need to launch Vim in the same directory as the database. This is particularly useful for projects where code is split into multiple subdirectories. Note: for this to work, you should build the database with absolute pathnames: cd to /, and do

find /my/project/dir -name '*.c' -o -name '*.h' > /foo/cscope.files

Then run Cscope in the same directory as the cscope.files file (or use 'cscope -i /foo/cscope.files'), then set and export the $CSCOPE_DB variable, pointing it to the cscope.out file that results):

cd /foo
cscope -b
CSCOPE_DB=/foo/cscope.out; export CSCOPE_DB   

(The last command above is for Bourne/Korn/Bash shells: I've forgotten how to export variables in csh-based shells, since I avoid them like the plague).

You should now be able to run 'vim -t foo' in any directory on your machine and have Vim jump right to the definition of 'foo'. I tend to write little shell scripts (that just define and export CSCOPE_DB) for all my different projects, which lets me switch between them with a simple 'source projectA' command.

like image 4
Shayan Pooya Avatar answered Oct 18 '22 12:10

Shayan Pooya