问题
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.
回答1:
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
回答2:
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
回答3:
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 dofind /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.
回答4:
You can create your cscpoe.files using absolute paths to your files, here is my scripts to generate my cscope databases
#!/bin/sh
find $PWD -name '*.[ch]' -exec echo \"{}\" \; | sort -u > cscope.files
cscope -bvq
Then just vim cscope.files and maybe :cs add cscope.out, although my cscope plugin does that automatically. Then I search for the files I am interested in and jump to them with gf.
回答5:
@Aaron H. is right.
For my configuration I used the cscope_maps.vim plugin and modified the following lines:
40 " add any cscope database in current directory
41 if filereadable("/usr/project/cscope.out")
42 cs add /usr/project/cscope.out /usr/project
43 " else add the database pointed to by environment variable
44 elseif $CSCOPE_DB != ""
45 cs add $CSCOPE_DB
46 endif
Where "/usr/project" is the location of the cscope.out file and the absolute path I want to use.
回答6:
Note: since I am not senior enough to add comments yet: Aaron and Neha's answers were useful to me to learn more about the entire system, but the best and most direct answer to the question is currently in 3rd place and is Shayan's.
The asker was more asking about getting absolute pathnames by cscope, not working around it using (the very capable and powerful) vim.
Note2: There is another way to make a single change in vim and accomplish the same as what Neha did.
:set cscopeprg=cscope -P path_to_relative_base
I like Neha's better, but this way is closer to modifying cscope rather than vim settings, if this is what you want. And this is the only way that allows you to move the cscope db to anywhere.
来源:https://stackoverflow.com/questions/2188405/how-to-let-cscope-use-absolute-path-in-cscope-out-file