0

I'm working on some project which uses C++17 standard with clangd-13.0. Sometime after I decided to add library that used C99 standard in its CMakeLists file and now clangd always does analysis based on a C99 standard even in cpp files.

My CMakeLists file looks like this:

cmake_minimum_required(VERSION 3.21)
project(my_proj)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

set(SOURCES include/some_header.h src/some_source.cpp)

# Adding library that mostly uses C code
add_subdirectory(lib/path/to/lib)
add_library(${PROJECT_NAME} STATIC ${SOURCES})
target_link_libraries(${PROJECT_NAME}
  imported_lib
)

Can I somehow exclude this library from compile_commands or enforce usage of C++17 standard?

Edit: After I've removed declaration of -std=c99 flag the problem still occurs, clangd analyses cpp code as pure C, even though compile_commands.json doesn't contain any -std parameter for library files

Psyhich
  • 47
  • 6
  • 1
    I believe you can set the `CXX_STANDARD` standard for a target you are building: [https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html](https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html) it would be like `set_property(TARGET myLibraryTargetName PROPERTY CXX_STANDARD 98)` – drescherjm Jan 21 '22 at 13:34
  • `add_subdirectory(lib/path/to/lib)` maybe the subdirectory changes the standard in a CMakeLists.txt using `set(CMAKE_CXX_STANDARD 98)` or edits the cpp command line flags: [https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html) – drescherjm Jan 21 '22 at 13:39
  • Already have tried set_property for both my project and library, but it doesn't help. compile_commands still has -std=C99 for library files and clangd keeps complaining at C++ code – Psyhich Jan 21 '22 at 13:50
  • 3
    This is not a complete example... _anything_ could be happening inside that `add_subdirectory` call and there's no `cmake_minimum_required()` at the top of the file to control which policies should be set. The `${SOURCES}` variable is empty, too. – Alex Reinking Jan 21 '22 at 21:06
  • To narrow down whether the issue is related to cmake or clangd, it would be helpful to see your generated `compile_commands.json` file (or at least some example entries from it), as well as [clangd logs](https://clangd.llvm.org/troubleshooting#gathering-logs) when opening a file that's parsed as the wrong language. – HighCommander4 Jul 12 '22 at 19:49

1 Answers1

0

As @drescherjm stated in comments, I only need to add set_property:

set_property(TARGET target_name PROPERTY CXX_STANDARD 17)
set_property(TARGET target_name PROPERTY CXX_STANDARD_REQUIRED ON)

Or using set_target_properties to assign both properties at once:

set_target_properties(target_name PROPERTIES 
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
)
Jixun
  • 140
  • 10
Psyhich
  • 47
  • 6