Skip to content

Instantly share code, notes, and snippets.

@HiImJulien
Created March 21, 2020 18:45
Show Gist options
  • Save HiImJulien/3eb47d7d874fe5483810bd77940e74c0 to your computer and use it in GitHub Desktop.
Save HiImJulien/3eb47d7d874fe5483810bd77940e74c0 to your computer and use it in GitHub Desktop.
Toolchain file to cross-compile from clang (WSL-Ubuntu) to Windows.
# Cross toolchain configuration for using clang-cl.
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0)
set(CMAKE_SYSTEM_PROCESSOR AMD64)
set(CMAKE_C_COMPILER "/usr/bin/clang-cl-9")
set(CMAKE_CXX_COMPILER "/usr/bin/clang-cl-9")
set(CMAKE_LINKER "/usr/bin/lld-link-9")
set(MSVC_BASE "/mnt/c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2019/Community/VC/Tools/MSVC/14.23.28105")
set(MSVC_INCLUDE "${MSVC_BASE}/include")
set(MSVC_LIB "${MSVC_BASE}/lib")
set(WINSDK_BASE "/mnt/c/Program\ Files\ \(x86\)/Windows\ Kits/10")
set(WINSDK_VER "10.0.18362.0")
set(WINSDK_INCLUDE "${WINSDK_BASE}/Include/${WINSDK_VER}")
set(WINSDK_LIB "${WINSDK_BASE}/lib/${WINSDK_VER}")
set(COMPILE_FLAGS
-D_CRT_SECURE_NO_WARNINGS
-imsvc "'${MSVC_INCLUDE}'"
-imsvc "'${WINSDK_INCLUDE}/ucrt'"
-imsvc "'${WINSDK_INCLUDE}/shared'"
-imsvc "'${WINSDK_INCLUDE}/um'"
-imsvc "'${WINSDK_INCLUDE}/winrt'"
)
string(REPLACE ";" " " COMPILE_FLAGS "${COMPILE_FLAGS}")
set(_CMAKE_C_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
set(CMAKE_C_FLAGS "${_CMAKE_C_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")
set(CMAKE_CXX_FLAGS "${_CMAKE_CXX_FLAGS_INITIAL} ${COMPILE_FLAGS}" CACHE STRING "" FORCE)
set(LINK_FLAGS
/manifest:no
-libpath:"${MSVC_LIB}/x64"
-libpath:"${WINSDK_LIB}/ucrt/x64"
-libpath:"${WINSDK_LIB}/um/x64"
)
string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
set(_CMAKE_EXE_LINKER_FLAGS_INITIAL "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "")
set(CMAKE_EXE_LINKER_FLAGS "${_CMAKE_EXE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
set(_CMAKE_MODULE_LINKER_FLAGS_INITIAL "${CMAKE_MODULE_LINKER_FLAGS}" CACHE STRING "")
set(CMAKE_MODULE_LINKER_FLAGS "${_CMAKE_MODULE_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
set(_CMAKE_SHARED_LINKER_FLAGS_INITIAL "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "")
set(CMAKE_SHARED_LINKER_FLAGS "${_CMAKE_SHARED_LINKER_FLAGS_INITIAL} ${LINK_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS ON)
@HiImJulien
Copy link
Author

You're welcome! :)

@johan-boule
Copy link

johan-boule commented Oct 9, 2022

One important thing to mention for people who stumble on this: this setup expects you to have a case insensitive filesystem mounted (which seems to be a usual setup with WSL where it has access to the host's NTFS filesystem).
If you need a fully autonomous setup, you need something like the original toolchain file which correctly deals with the case sensitivity of unix filesystems.

@vagran
Copy link

vagran commented Feb 1, 2024

set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_C_FLAGS}" CACHE STRING "")

Maybe it should be set(_CMAKE_CXX_FLAGS_INITIAL "${CMAKE_CXX_FLAGS}" CACHE STRING "")?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment