Skip to content

"No definition found" in C programming #1603

@wanghaiqiangk

Description

@wanghaiqiangk

I encountered this error message "No definition found" when invoking lsp-definition, which is bound to gd. Most of time, it works well. It's just sometimes not.

I have different LSP settings on two computers. I have tried different language servers like clangd and ccls. I also experimented with different clients, including vim-lsp (my primary plugin, thanks for the work!) and coc.nvim. While, no matter what combinations are, they all failed to jump to the definition.

Nethertheless, VSCode works quite good, using c/c++ intellisense extension. Frankly, I'm depressed about this comparison. But at the mean time, I'm curious about how LSP is working. I want to be helpful to improve but have no hints. So I post the question here.

Let me show some information and procedures on how to reproduce the problem.

  • OS: Ubuntu 18.04
  • Nvim: v0.9.5
  • clangd: 14.0.0
  • vim-lsp: 04428c9 (sha)

To reproduce, I use stb.

I create a driver program to setup cmake and generate compile_commands.json. Since the stb contains just header files, I believe this is mandatory to make LSP work.

#include <stdio.h>
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"

int main(int argc, char* argv[])
{
    printf("Hello World!\n");
    return 0;
}

The CMakeLists.txt looks like this.

cmake_minimum_required(VERSION 3.5)
project(stb-learn)

set(VERSION 1.0.0)

include(GNUInstallDirs)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# C
set(CMAKE_C_FLAGS "-Wall")

add_executable(${PROJECT_NAME} main.c)
set_target_properties(${PROJECT_NAME}
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

file(MAKE_DIRECTORY bin)

The compilation database is quite simple.

[
{
  "directory": "/home/wang/workspace/repos/stb/build",
  "command": "/usr/bin/cc   -Wall -g -o CMakeFiles/stb-learn.dir/main.c.o -I. -c /home/wang/workspace/repos/stb/main.c",
  "file": "/home/wang/workspace/repos/stb/main.c",
  "output": "CMakeFiles/stb-learn.dir/main.c.o"
}
]

Then open stb_ds.h and jump to line 1248. From there should be a line such as

    bucket = &table->storage[pos >> STBDS_BUCKET_SHIFT];

Try lsp-definition on the table keyword. The actual defintion locates at the same function block, at line 1235.

The logs of vim-lsp is uploaded as attachment.

vim-lsp.log

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions