Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clangd still cannot find header files after specifying the include options #393

Closed
iceypotato opened this issue Oct 14, 2022 · 11 comments
Closed

Comments

@iceypotato
Copy link

I am using Windows 11, and I have downloaded the gcc compiler from mingw-w64, which should include all the basic system headers, and I am using clangd for vscode.
For some reason, clangd still has a hard time trying to find all the basic header files, even when I specify multiple -isystemC:\path\to\includes. For example, clangd still fails to find bits/c++config.h.
I have made a .clangd file to specify the includes.

CompileFlags:
  Add: [
    -std=c++20,
    "-isystemC:\\Users\\me\\Documents\\x86_64-12.2.0-release-posix-seh-rt_v10-rev0\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\12.2.0\\include",
    "-isystemC:\\Users\\me\\Documents\\x86_64-12.2.0-release-posix-seh-rt_v10-rev0\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\12.2.0\\include\\c++",
    "-isystemC:\\Users\\me\\Documents\\x86_64-12.2.0-release-posix-seh-rt_v10-rev0\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\12.2.0\\include\\c++\\x86_64-w64-mingw32",
    "-isystemC:\\Users\\me\\Documents\\x86_64-12.2.0-release-posix-seh-rt_v10-rev0\\mingw64\\include",
    "-isystemC:\\Users\\me\\Documents\\x86_64-12.2.0-release-posix-seh-rt_v10-rev0\\mingw64\\x86_64-w64-mingw32\\include"
  ]

Am I missing something?
Sorry if this is not the place to get help with clangd.

@iceypotato iceypotato changed the title clangd still cannot find header files after specifying the compile commands clangd still cannot find header files after specifying the include options Oct 14, 2022
@HighCommander4
Copy link
Contributor

HighCommander4 commented Oct 14, 2022

Usually using --query-driver(as described towards the end of https://clangd.llvm.org/troubleshooting#cant-find-standard-library-headers-map-stdioh-etc) is an easier way to let clangd find the compiler's built-in includes than specifying them manually.

With this manual approach, I would guess the issue has something to do with the format of the path (e.g. spaces in the path, backslashes, use of quotes, etc. -- getting this stuff right can be tricky, especially on Windows). If you share clangd logs I may be able to offer a more specific diagnosis.

@iceypotato
Copy link
Author

I tried the query driver option and put the verbose log option. The query driver did not work. Here is the log from when I restarted the language server and clicked on a file.
clangd verbose.log

@HighCommander4
Copy link
Contributor

A couple of reasons --query-driver is not working in this case:

  1. --query-driver currently requires that the project has a compile_commands.json, please see https://clangd.llvm.org/installation#project-setup for ways to create one. (This is more a current limitation than a by-design requirement; I'm working in Detect system include paths when using compile_flags.txt clangd#1089 to relax this such that --query-driver will work with a compiler specified in clangd's config file even in the absence of compile_commands.json.)
  2. The argument for --query-driver should be the compiler executable, i.e. ..\bin\g++.exe, not just ..\bin

@iceypotato
Copy link
Author

Even with the compile_commands, it still doesn't work. I also fixed --query-driver to point to g++.exe.

This is my compile_commands.json

[
    { 
        "directory": "C:\\Users\\myuser\\Documents\\Visual Studio Code\\C",
        "command": "C:\\Users\\myuser\\Documents\\x86_64-12.2.0-release-posix-seh-rt_v10-rev0\\mingw64\\bin\\g++.exe",
        "file": "*"
    }
]

@HighCommander4
Copy link
Contributor

HighCommander4 commented Oct 15, 2022

Hmm, I don't think you can put * in "file", it needs to be a specific file for clangd to find the compile command.

I also wouldn't recommend writing the compile_commands.json manually, it's better to generate it. (Usually I recommend using bear but as I think users have reported it not working on Windows, you could try compiledb instead.)

@iceypotato
Copy link
Author

Yep, still no luck. I tried using compiledb, ran the compiledb make command and the outcome was still the same. It generated some stuff inside the compile_commands.json file, but it still could not find bits/c++config.h.

@HighCommander4
Copy link
Contributor

Please show new clangd logs with the generated compile_commands.json.

@iceypotato
Copy link
Author

clangd.verbose.log

I did some testing in a VM so the directories have changed.
compile_commands.json:

[
 {
  "directory": "C:\\Users\\myuser\\Documents\\Test",
  "arguments": [
   "g++",
   "-c",
   "-o",
   "testlib.o",
   "testlib.cpp"
  ],
  "file": "testlib.cpp"
 }
]

@HighCommander4
Copy link
Contributor

The logs are showing that:

  1. --query-driver is now working, based on the following:

I[13:10:41.424] System includes extractor: successfully executed C:\Users\myuser\Documents\x86_64-12.2.0-release-posix-seh-rt_v10-rev0\mingw64\bin\g++.exe

  1. There are no diagnostics issued when you open testlib.cpp.

There are diagnostics issued when you open <iostream>, the reason for that is:

I[13:10:41.362] Failed to find compilation database for c:\Users\myuser\Documents\x86_64-12.2.0-release-posix-seh-rt_v10-rev0\mingw64\lib\gcc\x86_64-w64-mingw32\12.2.0\include\c++\iostream

this can be fixed using https://clangd.llvm.org/faq#how-do-i-fix-errors-i-get-when-opening-headers-outside-of-my-project-directory.

@iceypotato
Copy link
Author

Thanks, it was fixed. I also had to add the compilers to my user path also. My question is, is there no other way to do it without a compiler_commands.json? Like, is there a way to do this with the clang user config file (.clangd, clangd.yaml)?

@HighCommander4
Copy link
Contributor

Once clangd/clangd#1089 is fixed, it should be enough to add:

CompileFlags:
  Compiler: /path/to/g++

to the clangd config file (and --query-driver=/path/to/g++ to the clangd command line), and compile_commands.json will not be required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants