3

vcpkg uses the GCC installation package by default on Linux. I see that the official document mentions the custom triplet method, but does not mention the clang tool chain.

How can I specify clang and libc++as the default tool chain of vcpkg?

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
endingly
  • 41
  • 1

1 Answers1

3

In order to force vcpkg to compile libraries with a specific toolchain, you can set the variable VCPKG_CHAINLOAD_TOOLCHAIN_FILE to an appropriate toolchain file inside a custom triplet file (aka overlay triplet).

For example, I manually installed clang-13 on my linux environment. The default CXX compiler is GCC.

This is the content of the clang-13-toolchain.cmake file I wrote:

set(CMAKE_C_COMPILER "/usr/bin/clang-13")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++-13")

message("clang-13 toolchain CMAKE_C_COMPILER = ${CMAKE_C_COMPILER}")
message("clang-13 toolchain CMAKE_CXX_COMPILER = ${CMAKE_CXX_COMPILER}")

And this is the content of the x64-linux-clang-13.cmake triplet file:

set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE static)
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE ${CMAKE_CURRENT_LIST_DIR}/../toolchains/clang-13-toolchain.cmake)

message("clang-13 triplet CMAKE_C_COMPILER = ${CMAKE_C_COMPILER}")
message("clang-13 triplet CMAKE_CXX_COMPILER = ${CMAKE_CXX_COMPILER}")

Assuming you have a cmake project and you use vcpkg in 'manifest mode', you must tell vcpkg to use your custom triplet (that uses the clang-13 toolchain), and tell cmake to also use the same toolchain.

So before you call project() in you main CMakeLists.txt, you must set the following:

  • CMAKE_TOOLCHAIN_FILE - points to the vcpkg toolchain file
  • VCPKG_OVERLAY_TRIPLETS - points to a directory containing custom triplets (x64-linux-clang-13.cmake)
  • VCPKG_TARGET_TRIPLET - should be set to "x64-linux-clang-13"
  • VCPKG_CHAINLOAD_TOOLCHAIN_FILE - should point to the location of clang-13-toolchain.cmake.

It is convenient to use CMakePresets.json file for this.

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37