Search code examples
ccmakeproject

Set up Clang-CL in Visual Studio with CMake


I'm trying to set up Clang-CL on Visual Studio. I need it because I was previously using C23 features on MinGW, but the debugging features MinGW provides on Windows were unusable and didn't help at all when I needed them. When I went to try to set it up with my CMakeLists.txt file after selecting the Clang-CL toolkit, I got this error: enter image description here

Severity    Code    Description Project File    Line    Suppression State   Details
Error       CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.27/Modules/Platform/Windows-Clang.cmake:170 (message):
  The current configuration mixes Clang and MSVC or some other CL compatible
  compiler tool.  This is not supported.  Use either clang or MSVC as both C,
  C++ and/or HIP compilers.     C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.27/Modules/Platform/Windows-Clang.cmake    170     

These are my CMakeLists.txt and CMakeSettings.json:

cmake_minimum_required(VERSION 3.21)

include(FetchContent)
find_package(OpenGL REQUIRED)

#- GLFW ---------------------------------------------------------------------

FetchContent_Declare(
    glfw
    GIT_REPOSITORY https://github.com/glfw/glfw
    GIT_TAG        3.3.8
    GIT_SHALLOW    TRUE
    GIT_PROGRESS   TRUE
)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "")
set(GLFW_BUILD_DOCS OFF CACHE BOOL "")
set(GLFW_INSTALL OFF CACHE BOOL "")
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "")
FetchContent_MakeAvailable(glfw)

#- GLAD ---------------------------------------------------------------------

FetchContent_Declare(
  glew
  GIT_REPOSITORY https://github.com/Perlmint/glew-cmake.git
  GIT_TAG        origin/master
)
FetchContent_GetProperties(glew)
if(NOT glew_POPULATED)
  FetchContent_Populate(glew)

  add_subdirectory(${glew_SOURCE_DIR} ${glew_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

#- STB ---------------------------------------------------------------------

FetchContent_Declare(
    stb
    GIT_REPOSITORY  https://github.com/nothings/stb.git
    GIT_TAG         master
    GIT_SHALLOW     TRUE
    GIT_PROGRESS    TRUE
)
FetchContent_GetProperties(stb)
if(NOT stb_POPULATED)
    FetchContent_Populate(stb)
    message("Fetching stb")

    add_library(stb_image INTERFACE ${stb_SOURCE_DIR}/stb_image.h)
    target_include_directories(stb INTERFACE ${stb_SOURCE_DIR})
endif()

#- FREETYPE -----------------------------------------------------------------

FetchContent_Declare(
    freetype
    GIT_REPOSITORY https://github.com/aseprite/freetype2.git
    GIT_TAG        VER-2-6-3 
)

FetchContent_GetProperties(freetype)
if(NOT freetype_POPULATED)
    FetchContent_Populate(freetype)
    add_subdirectory(${freetype_SOURCE_DIR} ${freetype_BINARY_DIR})
endif()

# --------------------


project(2dgfx C)

set(CMAKE_C_STANDARD 23)
# set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
        set(CMAKE_EXE_LINKER_FLAGS /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib)
endif()

include_directories(. ./deps/include ./lib)
link_directories(./deps/lib)
# link_libraries(freetype.lib glew32s.lib shell32.lib Gdi32.lib user32.lib opengl32.lib glfw3.lib)

add_executable(graphics
        deps/hash.c
        deps/hashfunc.c
        deps/vec.c
        lib/2dgfx.c
        main.c)

target_link_libraries(graphics PRIVATE glfw libglew_static stb_image)
{
    "configurations": [
        {
            "name": "x64-Debug",
            "generator": "Ninja",
            "configurationType": "Debug",
            "inheritEnvironments": [ "clang_cl_x64" ],
            "buildRoot": "${projectDir}\\out\\build\\${name}",
            "installRoot": "${projectDir}\\out\\install\\${name}",
            "cmakeCommandArgs": "",
            "buildCommandArgs": "",
            "ctestCommandArgs": "",
            "intelliSenseMode": "windows-clang-x64"
        }
    ]
}

I've tried to clean the directory, change the toolkits to something else, delete the cache, and many other things for the span of several hours. Nothing has worked.


Solution

  • I had CMAKE_C_COMPILER as clang-cl and CMAKE_CXX_COMPILER as clang++. Changing the latter to clang-cl solved the problem. That's weird because that would mean the toolkit that comes with Visual Studio is broken by default.