emacs-lsp / lsp-mode

Emacs client/library for the Language Server Protocol
https://emacs-lsp.github.io/lsp-mode
GNU General Public License v3.0
4.79k stars 890 forks source link

Missing OpenMP Headers for C/C++ using Clangd #3426

Open Xaldew opened 2 years ago

Xaldew commented 2 years ago

Thank you for the bug report

Bug description

Hello,

For some reason, I'm not able to get lsp-mode (using clangd) to find the OpenMP headers (omp.h), despite the server being properly setup and the compile_commands.json containing the appropriate flags (-fopenmp).

Truthfully though, I'm not sure if this is a lsp-mode problem or a clangd problem, so any assistance in narrowing this down would be appreciated!

Steps to reproduce

The simplest way to reproduce it would be to create a small C/C++ project with CMake:

CMakeLists.txt:

project(CCL C CXX)
cmake_minimum_required(VERSION 3.1)

find_package(OpenMP)

if (${OpenMP_FOUND})
  set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
else()
  set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -Wno-unknown-pragmas")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas -Wno-unused-private-field")
endif()

add_executable(main main.cpp)

main.cpp:

#include <omp.h>
#include <cstdio>

int main(void)
{
    int a[100] = {};

#pragma omp parallel for
    for (size_t i = 0; i < 100; ++i)
    {
        a[i]++;
    }

    int sum = 0;
    for (size_t i = 0; i < 100; i++)
    {
        sum += a[i];
    }

    printf("Sum=%d\n", sum);

    return 0;
}

Then build the project, e.g.:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local/ -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .

Expected behavior

OpenMP headers (omp.h) should be found.

Which Language Server did you use?

lsp-clangd

OS

Linux

Error callstack

N/A

Anything else?

N/A

Xaldew commented 2 years ago

After some further research, I'm pretty confident that this is this bug: clangd/clangd#971, so perhaps it would be a good idea to upgrade the internal clangd binary to 14.0.0?

If anyone else is reading this, a quick workaround is the upgrade the local version, e.g.:

  (use-package lsp-mode
    :ensure t
    :commands lsp
    :init
    (add-hook 'c-mode-hook      #'lsp)
    (add-hook 'c++-mode-hook    #'lsp)
    (setq lsp-clangd-version "14.0.0"))

And then calling M-x lsp-install-server "clangd"

knjmooney commented 2 years ago

Using clangd-14 wasn't sufficient to workaround the missing headers for me. I also had to install libomp-14. The package name is libomp-14-dev if using LLVM Debian/Ubuntu nightlies.