1

How can I make linker errors jumpable in quickfix?

Example:

int foo(void);

int main(int argc, char* argv[]) {
    foo();
}

Start up vim and run :set makeprg=gcc\ % and try :make. This produces a linker error and gives a press enter prompt. If I then check :copen the linker error is there but prefixed with || and can't be jumped to. I want to be able to jump to the file, or even the reference itself if possible, when linker errors like this occur.

  • 2
    :help errorformat – romainl Nov 16 '15 at 19:16
  • Note that a linker error does not contain a reference to a file and line (as a compiler error does), since it's all about combining compiled object files in the final binary: let's say a function definition is missing - where should the cursor jump? – VanLaser Nov 16 '15 at 22:12
  • @VanLaser it should just jump to the file or if it's really fancy to the first reference to the undefined symbol – Praxeolitic Nov 16 '15 at 22:27
  • The linker only reports that something is missing, or not in order, AFAIK ... at this stage, the source files were compiled alright, and the linker only sees that foo() has no definition. The idea is, linker errors are not necessarily a result of something written in your source code (to jump to). – VanLaser Nov 16 '15 at 22:32
  • 1
    @VanLaser Fair enough, I guess lack of main would be one example where there really is nowhere to jump. I'll see about figuring out errorformat and posting an answer. – Praxeolitic Nov 17 '15 at 8:33
1

You're asking to set up a search from the quickfix which as far as I know isn't possible via errorformat. However you could write a function to do it and then map that function to a keymap or a command or something.

E.g.

function JumpToLinkerError()
    let l:matches = matchlist(getline('.'), '^[^:]*.o:\([^:]*\):.*'
                                         \. "undefined reference to '\\([^']*\\)'")
    sbuffer l:matches[1]
    search('\<' . l:matches[2] . '\>')
endfunction

Although this makes some assumptions, such as the idea that 'test.c' is a buffer that you can switch to. Hopefully you can tweak to suit. It also doesn't handle any error conditions... Instead of sbuffer maybe sfind will work for you but that requires that you have set your path correctly and it seems likely to me that if there's a new linker error it's probably in a file you were just editing.

  • This also chokes if any of your filenames have colons in them but that sort of thing should be easy to tweak for platform specifics. – dash-tom-bang Feb 24 '16 at 22:50
  • Do you have a succinct argument for why this can't be done via errorformat? It would benefit the quality of the answer. I think I came to the same conclusion when I last looked into this but it's been a while so I forget if I had a clear and concrete reason for believing that. – Praxeolitic Feb 25 '16 at 10:29
  • The first sentence is my succinct answer as to why it's not possible. errorformat requires a line number for the jump and the linker error doesn't have one. – dash-tom-bang Feb 25 '16 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.