1

I want to add/expand the Clangd Language server, which I installed through Mason within Neovim, with the standard libraries that come with MinGW (I run windows 11). I'm trying to do CP, and for that I need <bits/stdc++.h> header file. The problem is, Clangd doesn't not recognize that header file and doesn't give me any intelli-sense & gives me warnings saying 'E447: Can't find file "std" in path', and things similar to it. I build the .cpp files using the command (keybinded) :!g++ Wall prac.cpp -o a.exe and it has no problem with the header file (I think because the compiler of MinGW is used here). Std is not recognized Header file is not recognized

I went through several threads regarding this. I tried the following:

  1. Tried to add an compile_commands.json file, but ended up not knowing where to add/save the file. (The bin of Mason in nvim-data 'C:\Users\Ankit\AppData\Local\nvim-data\mason\packages\clangd\clangd\bin' (\clangd\clangd is not typo) has only clangd.exe file in it) -- **couldn't figure how to tell clangd to find the header files from the MinGW folder.

  2. I was thinking of adding the header files themselves to the place Clang searches for header files but realised that Clang comes from LLVM (didn't do it).

Please see the followings attachments for verbose.

This is for Clang but it comes from LLVM

This is for clangd from Mason

This for my g++

I also have LLVM installed in my system. Hence, I want to add the header file in the LSP server if possible to remove the errors. (NOTE: The server works absolutely fine with headers like iostream, vector etc. I just want to add that header for CP only).

EDIT:: I want to know how I can add the compile_commands.json file for Clangd, which is installed through Mason in NVIM. I realise that this is more of a Mason and Clangd issue than it is an NVIM issue, but since Mason is a 'plugin' of Neovim, I believe that I can get some answers here.

2

2 Answers 2

0

The trick here is to use bear. Do the following:

Go to the root of your C++ project. Now use your building tool (like make), which you'd use to build your project and pick up the command which you'd normamlly invoke to build your project.

Example

Let's say that you have a Makefile in the root of your project and you'd simply just execute make to build your project. All you need to do is to execute bear -- make instead of make. This should generate a compile_comands.json for you in your project-root directory. Now just open up neovim and clangd will automatically read it.

0

I have tried so many things to solve this with clangd config, but couldn't find any working solution.

For me, the problem is only about the lsp diagnostics, because i usually just compile my source code with g++.

Now i just store the bits/stdc++.h header somewhere else, and automatically copy it if it doesn't exist. If you are using lspconfig, you can do the following.

local lspconfig = require("lspconfig")
local mason_registry = require("mason-registry")

-- clangd config
lspconfig.clangd.setup {
  filetypes = { "c", "cpp" },
  cmd = {
    'clangd',
    '--header-insertion=never',
  },
  capabilities = capabilities,
  on_attach = function(client, bufnr)
    on_attach(client, bufnr) -- your on_attach function
    local clangd = mason_registry.get_package("clangd")
    local install_path = clangd:get_install_path()
    local include_path = vim.fn.globpath(install_path, "**/include")
    if vim.fn.isdirectory(include_path .. "/bits") == 0 then -- when not found
      vim.fn.system("cp -r ~/.config/assets/clangd/bits " .. include_path)
      vim.defer_fn(function()
        pcall(vim.diagnostic.reset)
        vim.notify("Successfully created bit/stdc++.h header")
      end, 500)
    end
  end,
}
1
  • Thank you for writing an answer. This is a solution to a problem that should not exist and a totally misguided question (you are not blame for that, though). FYI, the bits/c++.h file is actually an implementation detail of GCC. No user code should ever rely on its existence. Write code that compiles without that file and you don't need to worry about it being there.
    – Friedrich
    Dec 19, 2023 at 10:01

Your Answer

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

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