Setting up clang-cl to work with CMake and Ninja

Are you a C/C++ developer looking to explore alternative compilers or achieve cross-platform builds? Look no further! This post dives deep into configuring clang-cl, a powerful compiler from the LLVM project, to work seamlessly with CMake, a popular build system, and Ninja, a fast build tool.

Whether you’re seeking modern compiler features, improved performance, or a more customizable build experience, this guide equips you with the knowledge to harness the power of this trio. We’ll walk you through the setup process step-by-step, ensuring a smooth transition and unlocking the potential of clang-cl for your development workflow.

You have to modify the cmake-tools-kits.json to add the "environmentSetupScript" option.

  {
    "name": "Clang-cl 17.0.6 x86_64-pc-windows-msvc",
    "compilers": {
      "C": "D:\\Program Files\\LLVM\\bin\\clang-cl.exe",
      "CXX": "D:\\Program Files\\LLVM\\bin\\clang-cl.exe"
    },
    "environmentSetupScript": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Auxiliary/Build/vcvars64.bat",
    "isTrusted": true
  }

My cmake-tools-kits.json file looks like this

[
  {
    "name": "Clang 17.0.6 x86_64-pc-windows-msvc",
    "compilers": {
      "C": "D:\\Program Files\\LLVM\\bin\\clang.exe",
      "CXX": "D:\\Program Files\\LLVM\\bin\\clang++.exe"
    },
    "isTrusted": true
  },
  {
    "name": "Clang-cl 17.0.6 x86_64-pc-windows-msvc",
    "compilers": {
      "C": "D:\\Program Files\\LLVM\\bin\\clang-cl.exe",
      "CXX": "D:\\Program Files\\LLVM\\bin\\clang-cl.exe"
    },
    "environmentSetupScript": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Auxiliary/Build/vcvars64.bat",
    "isTrusted": true
  },
  {
    "name": "GCC 13.1.0 x86_64-w64-mingw32",
    "compilers": {
      "C": "D:\\Program Files\\w64devkit\\bin\\gcc.exe",
      "CXX": "D:\\Program Files\\w64devkit\\bin\\g++.exe"
    },
    "isTrusted": true
  },
  {
    "name": "Visual Studio Build Tools 2022 Release - amd64",
    "visualStudio": "af227408",
    "visualStudioArchitecture": "x64",
    "isTrusted": true,
    "preferredGenerator": {
      "name": "Visual Studio 17 2022",
      "platform": "x64",
      "toolset": "host=x64"
    }
  },
  {
    "name": "Visual Studio Build Tools 2022 Release - amd64_x86",
    "visualStudio": "af227408",
    "visualStudioArchitecture": "x64",
    "isTrusted": true,
    "preferredGenerator": {
      "name": "Visual Studio 17 2022",
      "platform": "win32",
      "toolset": "host=x64"
    }
  },
  {
    "name": "Visual Studio Build Tools 2022 Release - x86",
    "visualStudio": "af227408",
    "visualStudioArchitecture": "x86",
    "isTrusted": true,
    "preferredGenerator": {
      "name": "Visual Studio 17 2022",
      "platform": "win32",
      "toolset": "host=x86"
    }
  },
  {
    "name": "Visual Studio Build Tools 2022 Release - x86_amd64",
    "visualStudio": "af227408",
    "visualStudioArchitecture": "x86",
    "isTrusted": true,
    "preferredGenerator": {
      "name": "Visual Studio 17 2022",
      "platform": "x64",
      "toolset": "host=x86"
    }
  }
]

In addition, you have to make settings.json file for the VSCode CMake extension and pass the following options (Mandatory!)

    "cmake.configureArgs": [
        "-DCMAKE_C_COMPILER=clang-cl", 
        "-DCMAKE_CXX_COMPILER=clang-cl", 
        "-DCMAKE_GENERATOR=Ninja", // Optional
        "-DCMAKE_MAKE_PROGRAM=D:/Program Files/cmake-3.28.1-windows-x86_64/bin/ninja.exe", // Optional
        "-DCMAKE_INSTALL_PREFIX=D:/usr/local", // Optional
    ]
}

Note that Ninja generator does not ship with the CMake. You have to download the Ninja generator and put it in the path. Here I have put it inside the path where cmake.exe lives.

If you were to do all these steps manually you would open up Developer Command Prompt for VS 2022 which loads vcvars64.bat script (actually loads bunch of environment variables necessary for the Microsoft visual studio compiler) then run the following command in the path where your project and CMakeLists.txt file exists

cmake -G "Ninja" -B build -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments