Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate VS2022 project files with CMake which use clang

Tags:

c++

cmake

clang

I am trying to generate Visual Studio 2022 project files using CMake and the clang compiler.

Currently, if I call:

cmake -S . -B build -G "Ninja Multi-Config" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

I can compile and build my project fine using Ninja.

Now, I have tried to get this to work using VS2022, however if I simply just call:

cmake -S . -B build -G "Visual Studio 17 2022" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

it compiles the project using msvc, which is not intended.

Now, if i change this to include the toolchain:

cmake -S . -B build -G "Visual Studio 17 2022" -T="ClangCL" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

It says that it can't find the compilers specified. If I swap these with the path to the specified compilers, it will seemingly run, but it will generate an error in the CMakeErrors.txt, which says:

clang: error: no input files.

So, my question is, can anyone help me to build VS2022 project files that compile with clang using CMake?

my CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.16)
project(Test)

# Use C23 standard
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

# Use C++23 standard
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Compiler setting
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")

# Include subprojects
add_subdirectory(TestLib)
add_subdirectory(Runtime)
like image 704
Tea-F-Tea Avatar asked Jan 24 '26 16:01

Tea-F-Tea


1 Answers

Apparently, you can include a FILEPATH="" to the command.

So, changing to the following command:

cmake -S . -B build -G "Visual Studio 17 2022" -T="ClangCL" -D_CMAKE_TOOLCHAIN_PREFIX="ClangCL" -DCMAKE_C_COMPILER:FILEPATH="C:\Program Files\LLVM\bin\clang.exe" -DCMAKE_CXX_COMPILER:FILEPATH="C:\Program Files\LLVM\bin\clang++.exe"

build my project succesfully

EDIT:

after I deleted my build files and rebuild using the above command, i get a new error:

  Microsoft.CppCommon.targets(1575, 5): [MSB6006] "llvm-lib.exe" exited with code 1.

When i search for this, i read something about this being a common issue in an earlier build of VS2022 when building static libraries. Currently, I am on 17.6.4.

However, when I use set LTCG="No", the same error occurs.

like image 196
Tea-F-Tea Avatar answered Jan 26 '26 09:01

Tea-F-Tea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!