'CMAKE clang variables

I have this piece of code.

FIND_PACKAGE(Clang REQUIRED)
SET(CMAKE_C_COMPILER clang)

I would like to set clang as C compiler, but if I use it like this, it uses clang from system and not from FIND_PACKAGE. FIND_PACKAGE should set some variables like CLANG_INCLUDE_DIRS. I need variable with path to clang, but I have not found name of that variable or other variables. Where can I find it or what is the name of that variable ?

I use cmake 3.18



Solution 1:[1]

By itself, find_package(Clang) searches only clang libraries, not a compiler. So it doesn't set any variable which points directly to the compiler.

The closest thing it sets is variable LLVM_INSTALL_PREFIX, which denotes the LLVM installation prefix. (This variable is set in FindLLVM.cmake script, which is called by FindClang.cmake).

So you could set the path to the C compiler with

# Instead of "Clang" package one could search "LLVM".
FIND_PACKAGE(Clang REQUIRED)
SET(CMAKE_C_COMPILER ${LLVM_INSTALL_PREFIX}/bin/clang)

Make sure to place this setting before the project() call, as described in that answer.

Solution 2:[2]

In the end, I have done it with this snipped of code. FIND_PACKAGE is not needed.

IF (NOT ${CMAKE_C_COMPILER_ID} STREQUAL "Clang")
    MESSAGE(FATAL_ERROR "Set clang as C compiler")
ENDIF()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tsyvarev
Solution 2 Zakys98