clangd: used compiler ignored from compile_commands.json?

I use g++ for building because clang still miss features of C++23 that I want to use (especially the u8"" string literals that are ackward in C++20 but fixed in C++23, but my installed clang don’t provide <filesystem> header and then clangd generate lot of errors )

my concern is that in compile_commands.json g++ is clearly specified, so clangd should use gcc’s headers but seems to use clang’s header and library

$ cat ../compile_commands.json 
[
{
  "directory": "SomePath/build",
  "command": "/usr/bin/g++ -DBOOST_SYSTEM_DYN_LINK -DBOOST_SYSTEM_NO_LIB -DBOOST_THREAD_DYN_LINK -DBOOST_THREAD_NO_LIB -ISomePath/3rdParties/sha256 -std=c++23 -o CMakeFiles/synctegrity.dir/main.cpp.o -c SomePath/main.cpp",
  "file": "SomePath/main.cpp"
},
{
  "directory": "SomePath/build",
  "command": "/usr/bin/g++ -DBOOST_SYSTEM_DYN_LINK -DBOOST_SYSTEM_NO_LIB -DBOOST_THREAD_DYN_LINK -DBOOST_THREAD_NO_LIB -ISomePath/3rdParties/sha256 -std=c++23 -o CMakeFiles/synctegrity.dir/Conf.cpp.o -c SomePath/Conf.cpp",
  "file": "SomePath/Conf.cpp"
},
....

CocDebug output

../main.cpp|1 col 1 error| [clang drv_invalid_value] Invalid value 'c++23' in '-std=c++23' [E]
../main.cpp|8 col 10-30 error| [clang expected_namespace_name] In included file: expected namespace name [E]
../main.cpp|19 col 23-25 error| [clang undeclared_var_use] Use of undeclared identifier 'fs' [E]
../main.cpp|32 col 18-20 error| [clang undeclared_var_use] Use of undeclared identifier 'fs' [E]
../main.cpp|38 col 13-15 error| [clang undeclared_var_use] Use of undeclared identifier 'fs' [E]
../main.cpp|43 col 18-20 error| [clang undeclared_var_use] Use of undeclared identifier 'fs' [E]
../main.cpp|51 col 5-7 error| [clang undeclared_var_use] Use of undeclared identifier 'fs' [E]
../main.cpp|71 col 9-11 error| [clang undeclared_var_use] Use of undeclared identifier 'fs' [E]
../main.cpp|73 col 13-15 error| [clang undeclared_var_use] Use of undeclared identifier 'fs' [E]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'gnu++2b' for 'Working draft for ISO C++ 2023 DIS with GNU extensions' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'c++2b' for 'Working draft for ISO C++ 2023 DIS' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'gnu++20' for 'ISO C++ 2020 DIS with GNU extensions' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'c++20' for 'ISO C++ 2020 DIS' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'gnu++17' for 'ISO C++ 2017 with amendments and GNU extensions' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'c++17' for 'ISO C++ 2017 with amendments' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'gnu++14' for 'ISO C++ 2014 with amendments and GNU extensions' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'c++14' for 'ISO C++ 2014 with amendments' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'gnu++11' for 'ISO C++ 2011 with amendments and GNU extensions' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'c++11' for 'ISO C++ 2011 with amendments' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'gnu++98' or 'gnu++03' for 'ISO C++ 1998 with amendments and GNU extensions' standard [I]
../main.cpp|1 col 1 info| [clang note_drv_use_standard] Use 'c++98' or 'c++03' for 'ISO C++ 1998 with amendments' standard [I]

but as we can see in the compile_commands.json generated by cmake, there is -std=c++23 and no c++23… so I don’t understand the first error… and others errors are normal since I set in CMakeLists.txt

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) # -std=c++11 instead of -std=gnu++11
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

second line error is generated by the following code

#include <filesystem>
namespace fs = std::filesystem;

and the reason given by clangd is

use of undeclared identifier 'std' (ccls 2)
 ─────────────────────────────────────────────────────────────
Use of undeclared identifier 'std' (clang undeclared_var_use)

ok… clangd don’t reconized std namespace???

System information

$ clangd --version clangd version 15.0.6 Features: linux Platform: x86_64-pc-linux-gnu

Editor/LSP plugin: coc-vim + coc-clangd plugin

Operating system: linux-gentoo

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 37

Most upvoted comments

@jlmxyz, you can use clangd’s config file to remove -std=c++23 and add -std=c++2b, like so:

CompileFlags:
  Remove:
  - '-std=c++23'
  Add:
  - '-std=c++2b'

by the way… there miss some doc into https://releases.llvm.org/10.0.0/tools/clang/tools/extra/docs/clangd/Configuration.html#configuration on how to use the --query-driver… (ie that ** will replace any path and that we can use * for word globing…)