2

I'm separating the functions from main.c to func1.c func2.c, then I'm using util.h to store the prototype (all name of all functions).

PROBLEM

when I try to go to implementation, lsp leading me into util.c, but i want to leading to func1.c

main.c

#include "util.h"

int main(){
  int x = fun||c1(); // here || is cursure location !
  int y = funct2();
  //...
}

util.c

#define TRUE 1
#define FALSE 0
int func1();
int func2();
//...

func1.c

int  func1(){
  //...
}

Makefile

all: main

main: func1.o func2.o ... main.o
    gcc func1.o func2.o ... main.o -o main

func1.o: func1.c util.h
    gcc func1.c

func2.o: func2.c util.h
    gcc func2.c
#...

my env:

i use:

  • neovim as ide
  • lspconfig plugin handle lsp
  • ccls/clangd is tow lsp for c/cpp
  • :lua vim.lsp.buf.implementation() go to implementation but lead me to util.h
  • :lua vim.lsp.buf.definition() go to definition but lead me to util.h too

3 Answers 3

1

The location of the implementation depends on the way the project is built. You can tell clangd about this using compile_commands.json.

You can generate this file using bear: bear -- make.

Bear is probably available in your package manager.

2

As far as I know, clangd is not able to jump to func1() via "Go To Definition" or "Find References" capabilities because there is not a direct link to func1.c.

As an alternative, you can use tags. You will need to install one of the flavors available (:h ctags). I recommend the package universal-ctags. Run ctags -R . in your root directory and simply type <c-]> to go to the first match or g <c-] to show a list if there are several matches.

0

Adding this here for clang users who also want neovim LSP go-to functions to work on C projects built with CMake.

  1. You can have CMake generate a compile_commands.json as follows:

    cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ..
    
  2. Move this new file from your build directory (if applicable) to the root of you project.

  3. If new source files are added to your project you may need to rerun 1 & 2.

I found this got vim.lsp.buf.definition() and vim.lsp.buf.references() working in my C project when lots of other tries failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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