clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
592 stars 97 forks source link

Clangd cannot find headers from a linked library for cuda files #632

Closed HansLjy closed 1 month ago

HansLjy commented 1 month ago

I am not sure whether this is a problem of clangd or vscode-clangd. This is a minimum (non-)working example:

File structure:

.
├── CMakeLists.txt
├── main
├── main.cc
├── main.cu
└── main.hpp

CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(cmake_and_cuda LANGUAGES CXX CUDA)

find_package(Eigen3 3.4.0 EXACT REQUIRED)

add_executable(main1 main.cc)
target_link_libraries(main1 Eigen3::Eigen)

add_executable(main2 main.cu)
target_link_libraries(main2 Eigen3::Eigen)

main.cc:

#include "Eigen/Dense"

int main() {
}

main.cu:

#include "main.hpp"
#include "Eigen/Dense"

int main() {
}

Both targets (main1 and main2) can compile successfully. However, vscode shows the following error for main.cu: image

On the contrary, there is no error for main.cc, so I suppose it has something to do with CUDA files.

The following is the log of clangd copied from the Output:

I[11:20:36.022] Ubuntu clangd version 14.0.0-1ubuntu1.1
I[11:20:36.022] Features: linux+grpc
I[11:20:36.022] PID: 31308
I[11:20:36.022] Working directory: /home/hansljy/tmp/test
I[11:20:36.022] argv[0]: /usr/bin/clangd
I[11:20:36.022] argv[1]: -header-insertion=never
I[11:20:36.022] Starting LSP over stdin/stdout
I[11:20:36.022] <-- initialize(0)
I[11:20:36.022] --> reply:initialize(0) 0 ms
I[11:20:36.023] <-- initialized
I[11:20:36.026] <-- textDocument/didOpen
I[11:20:36.026] --> textDocument/publishDiagnostics
I[11:20:36.026] Loaded compilation database from /home/hansljy/tmp/test/build/compile_commands.json
I[11:20:36.026] --> window/workDoneProgress/create(0)
I[11:20:36.026] Enqueueing 2 commands for indexing
I[11:20:36.026] ASTWorker building file /home/hansljy/tmp/test/main.cu version 1 with command 
[/home/hansljy/tmp/test/build]
/usr/local/cuda-11.8/bin/nvcc -forward-unknown-to-host-compiler --options-file --generate-code=arch=compute_86,code=[compute_86,sm_86] -c -std=c++17 -resource-dir=/usr/lib/llvm-14/lib/clang/14.0.0 -- /home/hansljy/tmp/test/main.cu
I[11:20:36.027] <-- reply(0)
I[11:20:36.027] --> $/progress
I[11:20:36.027] --> $/progress
I[11:20:36.031] --> textDocument/clangd.fileStatus
I[11:20:36.140] --> $/progress
I[11:20:36.140] --> $/progress
I[11:20:36.249] <-- textDocument/documentLink(1)
I[11:20:36.249] <-- clangd/inlayHints(2)
I[11:20:36.291] --> workspace/semanticTokens/refresh(1)
I[11:20:36.291] --> textDocument/clangd.fileStatus
I[11:20:36.291] <-- reply(1)
I[11:20:36.305] --> textDocument/publishDiagnostics
I[11:20:36.306] --> reply:textDocument/documentLink(1) 56 ms
I[11:20:36.306] --> reply:clangd/inlayHints(2) 56 ms
I[11:20:36.306] --> textDocument/clangd.fileStatus
I[11:20:36.527] <-- textDocument/documentSymbol(3)
I[11:20:36.527] --> reply:textDocument/documentSymbol(3) 0 ms
I[11:20:36.527] --> textDocument/clangd.fileStatus
I[11:20:36.809] <-- textDocument/documentLink(4)
I[11:20:36.810] --> reply:textDocument/documentLink(4) 0 ms
I[11:20:36.810] --> textDocument/clangd.fileStatus
I[11:20:37.489] <-- $/setTrace
I[11:20:37.489] unhandled notification $/setTrace
I[11:20:37.644] <-- $/setTrace
I[11:20:37.644] unhandled notification $/setTrace
I[11:20:37.762] <-- textDocument/codeAction(5)
I[11:20:37.762] --> reply:textDocument/codeAction(5) 0 ms
I[11:20:37.762] --> textDocument/clangd.fileStatus
I[11:20:37.763] <-- $/cancelRequest
I[11:20:37.763] <-- textDocument/codeAction(6)
I[11:20:37.763] <-- $/cancelRequest
I[11:20:37.763] <-- textDocument/codeAction(7)
I[11:20:37.763] --> reply:textDocument/codeAction(6) 0 ms, error: Task was cancelled.
I[11:20:37.763] --> reply:textDocument/codeAction(7) 0 ms
I[11:20:37.763] --> textDocument/clangd.fileStatus
[Error - 11:20:37] Request textDocument/codeAction failed.
[object Object]
I[11:20:37.778] <-- $/setTrace
I[11:20:37.778] unhandled notification $/setTrace
I[11:20:37.848] <-- $/setTrace
I[11:20:37.848] unhandled notification $/setTrace
I[11:20:38.013] <-- textDocument/codeAction(8)
I[11:20:38.013] --> reply:textDocument/codeAction(8) 0 ms
I[11:20:38.013] --> textDocument/clangd.fileStatus
I[11:20:38.014] <-- textDocument/documentSymbol(9)
I[11:20:38.014] --> reply:textDocument/documentSymbol(9) 0 ms
I[11:20:38.014] --> textDocument/clangd.fileStatus

System Info

I use Ubuntu 22.04. The version for clangd is 14.0.0.

HighCommander4 commented 1 month ago

Where is the header Eigen/Dense located in the filesystem?

Can you show a log for opening main.cc?

HansLjy commented 1 month ago

Thanks for the reply. Eigen/Dense is located in /usr/local/include/eigen3/Eigen/Dense. Below is the log for opening main.cc:

I[09:32:56.664] Ubuntu clangd version 14.0.0-1ubuntu1.1
I[09:32:56.664] Features: linux+grpc
I[09:32:56.664] PID: 84591
I[09:32:56.665] Working directory: /home/hansljy/tmp/test
I[09:32:56.665] argv[0]: /usr/bin/clangd
I[09:32:56.665] argv[1]: -header-insertion=never
I[09:32:56.665] Starting LSP over stdin/stdout
I[09:32:56.665] <-- initialize(0)
I[09:32:56.665] --> reply:initialize(0) 0 ms
I[09:32:56.666] <-- initialized
I[09:32:56.668] <-- textDocument/didOpen
I[09:32:56.669] --> textDocument/publishDiagnostics
I[09:32:56.669] Loaded compilation database from /home/hansljy/tmp/test/build/compile_commands.json
I[09:32:56.669] --> window/workDoneProgress/create(0)
I[09:32:56.669] ASTWorker building file /home/hansljy/tmp/test/main.cc version 1 with command 
[/home/hansljy/tmp/test/build]
/usr/bin/c++ --driver-mode=g++ -isystem /usr/local/include/eigen3 -o CMakeFiles/main1.dir/main.cc.o -c -std=c++17 -resource-dir=/usr/lib/llvm-14/lib/clang/14.0.0 -- /home/hansljy/tmp/test/main.cc
I[09:32:56.669] Enqueueing 2 commands for indexing
I[09:32:56.669] <-- reply(0)
I[09:32:56.669] --> $/progress
I[09:32:56.669] --> $/progress
I[09:32:56.670] --> textDocument/clangd.fileStatus
I[09:32:56.729] <-- textDocument/documentLink(1)
I[09:32:56.730] <-- clangd/inlayHints(2)
I[09:32:56.788] --> $/progress
I[09:32:56.788] --> $/progress
I[09:32:56.840] <-- textDocument/codeAction(3)
I[09:32:56.840] <-- $/cancelRequest
I[09:32:56.840] <-- textDocument/codeAction(4)
I[09:32:56.840] <-- $/cancelRequest
I[09:32:56.840] <-- textDocument/codeAction(5)
I[09:32:56.840] <-- $/setTrace
I[09:32:56.840] unhandled notification $/setTrace
I[09:32:56.850] <-- $/setTrace
I[09:32:56.850] unhandled notification $/setTrace
I[09:32:56.902] <-- $/setTrace
I[09:32:56.902] unhandled notification $/setTrace
I[09:32:57.060] <-- textDocument/documentSymbol(6)
I[09:32:57.060] <-- $/cancelRequest
I[09:32:57.060] <-- $/cancelRequest
I[09:32:57.060] <-- textDocument/codeAction(7)
I[09:32:57.083] <-- textDocument/documentLink(8)
I[09:32:57.351] --> workspace/semanticTokens/refresh(1)
I[09:32:57.352] <-- reply(1)
I[09:32:57.357] --> textDocument/publishDiagnostics
I[09:32:57.357] --> reply:textDocument/documentLink(1) 627 ms, error: Task was cancelled.
I[09:32:57.357] --> reply:clangd/inlayHints(2) 627 ms
I[09:32:57.357] --> reply:textDocument/codeAction(3) 517 ms, error: Task was cancelled.
I[09:32:57.357] --> reply:textDocument/codeAction(4) 517 ms, error: Task was cancelled.
I[09:32:57.357] --> reply:textDocument/codeAction(5) 517 ms, error: Task was cancelled.
I[09:32:57.357] --> reply:textDocument/documentSymbol(6) 296 ms
I[09:32:57.357] --> reply:textDocument/codeAction(7) 296 ms
I[09:32:57.357] --> reply:textDocument/documentLink(8) 273 ms
I[09:32:57.357] --> textDocument/clangd.fileStatus
[Error - 09:32:57] Request textDocument/documentLink failed.
[object Object]
[Error - 09:32:57] Request textDocument/codeAction failed.
[object Object]
[Error - 09:32:57] Request textDocument/codeAction failed.
[object Object]
[Error - 09:32:57] Request textDocument/codeAction failed.
[object Object]
I[09:32:58.525] <-- clangd/inlayHints(9)
I[09:32:58.525] --> reply:clangd/inlayHints(9) 0 ms
I[09:32:58.525] --> textDocument/clangd.fileStatus
I[09:33:00.548] <-- textDocument/codeAction(10)
I[09:33:00.548] --> reply:textDocument/codeAction(10) 0 ms
I[09:33:00.548] --> textDocument/clangd.fileStatus
I[09:33:00.548] <-- textDocument/documentSymbol(11)
I[09:33:00.549] --> reply:textDocument/documentSymbol(11) 0 ms
I[09:33:00.549] --> textDocument/clangd.fileStatus
HighCommander4 commented 1 month ago

Well, it looks like the generated compile command for main.cc contains -isystem /usr/local/include/eigen3, and the one for main.cu does not.

From the log, for main.cc:

I[09:32:56.669] ASTWorker building file /home/hansljy/tmp/test/main.cc version 1 with command [/home/hansljy/tmp/test/build] /usr/bin/c++ --driver-mode=g++ -isystem /usr/local/include/eigen3 -o CMakeFiles/main1.dir/main.cc.o -c -std=c++17 -resource-dir=/usr/lib/llvm-14/lib/clang/14.0.0 -- /home/hansljy/tmp/test/main.cc

and for main.cu:

I[11:20:36.026] ASTWorker building file /home/hansljy/tmp/test/main.cu version 1 with command [/home/hansljy/tmp/test/build] /usr/local/cuda-11.8/bin/nvcc -forward-unknown-to-host-compiler --options-file --generate-code=arch=compute_86,code=[compute_86,sm_86] -c -std=c++17 -resource-dir=/usr/lib/llvm-14/lib/clang/14.0.0 -- /home/hansljy/tmp/test/main.cu

It's the absence of this flag that's causing clangd to be unable to resolve the Eigen/Dense include.

HansLjy commented 1 month ago

Seems reasonable. But how can I tell clangd to add this flag? Do I need to modify CMakeLists.txt manually so that it generates the correct compile_commands.json?

HighCommander4 commented 1 month ago

Assuming the flag is indeed missing from compile_commands.json, the options are:

  1. Arrange for compile_commands.json to contain the flag. I'm not really in a position to give specific advice here as this is more a question about the tool that generates compile_commands.json (CMake in this case).
  2. Use a .clangd config file to add the flag, with something like:
    CompileFlags:  
     Add: [-isystem, /usr/local/include/eigen3]
HansLjy commented 1 month ago

Thanks for the advice. I found a solution on the cmake forum(the last reply): simply add

set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_INCLUDES 0)
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_OBJECTS 0)

to the top-level CMakeLists.txt. I am not sure about the side effects of this, but it solves my problem.