cmakeclangninja

Output executable with clang, cmake and ninja on Windows


With the following CMakeLists.txt

cmake_minimum_required(VERSION 3.30)

project(usage_example)

add_executable(usage main.cpp)

And the following command:

cmake -S . -B build -G Ninja -DCMAKE_C_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe" -DCMAKE_CXX_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe" -DCMAKE_C_COMPILER_ID="Clang" -DCMAKE_CXX_COMPILER_ID="Clang" -DCMAKE_SYSTEM_NAME="Generic"
cmake --build build

It builds without issue, but the resulting binary doesn't include the exe file type, it simply produces build\usage

If I rename usage to usage.exe it works without issue, but why is this extra step needed?


Solution

  • You set CMAKE_SYSTEM_NAME variable to "Generic". According to documentation, it means

    Some platforms, e.g. bare metal embedded devices

    That is, you tell CMake that your target system is not Windows. So CMake doesn't use .exe extension for your executable.

    For build your project for Windows, you may set CMAKE_SYSTEM_NAME variable to "Windows". Or just leave that variable unset, so CMake will automatically detect your host system, and will build the project for it.