0

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

Zakys98
  • 53
  • 9
  • 2
    `find_package(clang` when you want to _build_ a program that _uses_ clang _library_, like a compiler plugin or some stuff from llvm. Just `set(CMAKE_C_COMPILER /the/path/to/the/clang/you/want/to/use/clang)`. There is no "clang from find_package", or it's the same clang. I think. – KamilCuk Mar 31 '22 at 12:38
  • @KamilCuk I want find_package to find clang and use that clang from find_package and i do not think this is how i can do it – Zakys98 Mar 31 '22 at 12:43

2 Answers2

0

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
0

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()
Zakys98
  • 53
  • 9