sublimelsp / LSP-clangd

C/C++ support for Sublime's LSP plugin provided through clangd.
MIT License
43 stars 1 forks source link

Does not recognize c++20 modules #17

Closed r2com closed 1 year ago

r2com commented 1 year ago

I got a project in Sublime with CMake which produces compile_commands.json basically main.cpp is main file, and foo.cpp is a module exported and imported and used in main.cpp

all compiles OK with GCC and LSP works but shows: clang(module_not_found) on "import foo;" in main.cpp

here is the compile_commands.json

[
{
  "directory": "/home/r2com/projects/prog/cpp/build",
  "command": "/usr/bin/c++    -std=c++2b -fmodules-ts -std=c++23 -o CMakeFiles/hello.dir/main.cpp.o -c /home/r2com/projects/prog/cpp/src/main.cpp",
  "file": "/home/r2com/projects/prog/cpp/src/main.cpp"
},
{
  "directory": "/home/r2com/projects/prog/cpp/build",
  "command": "/usr/bin/c++    -std=c++2b -fmodules-ts -std=c++23 -o CMakeFiles/hello.dir/foo.cpp.o -c /home/r2com/projects/prog/cpp/src/foo.cpp",
  "file": "/home/r2com/projects/prog/cpp/src/foo.cpp"
}
]

and here is Cmake:

cmake_minimum_required(VERSION 3.25)
project(std_module_example CXX)

# Default to C++ extensions being off. Clang's modules support has trouble
# with extensions right now and it is not required for any other compiler
set(CMAKE_CXX_EXTENSIONS OFF)

# Set the C++ standard to C++20
set(CMAKE_CXX_STANDARD 23)

# Enable C++ modules by setting the appropriate compiler flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2b")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmodules-ts")

# Create the foo module
add_library(foo MODULE foo.cpp)

# Create an interface library for the foo module
add_library(foo_interface INTERFACE)
target_link_libraries(foo_interface INTERFACE foo)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Create the hello executable
add_executable(hello main.cpp foo.cpp)  # <-- Include foo.cpp here

# Link the foo_interface to the hello executable
target_link_libraries(hello PRIVATE foo_interface)

# Set the RPATH for the hello executable when building
set_target_properties(hello PROPERTIES
    BUILD_RPATH "$ORIGIN"
)

why LSP-Clangd cannot figure it out?

LDAP commented 1 year ago

The plugin only integrates clangd, so you might get better help on the clangd github.

Maybe you have an older version of clangd installed? In this case that version would be preferred.

r2com commented 1 year ago

well, nah, clangd people say that modules are not supported yet. so bummer. time to get on Clion ;)