Hi all,
I’m looking to get my CMake project set up to cross-compile a library for Linux from a Windows host. Because what I’m compiling has to be compatible with a specific version of the Unreal Engine, I want to use the same version of the toolchain that Epic requires for cross-compiling Unreal Engine itself. The toolchains are listed on this page, and I’m currently using v17 for UE 4.26.
So far, I have set up a CMake toolchain file (below) to make use of the Clang toolchain. LINUX_MULTIARCH_ROOT
is an environment variable that is set up when the toolchain is installed.
if("$ENV{LINUX_MULTIARCH_ROOT}" STREQUAL "")
message(FATAL_ERROR "Provide the path to the Clang toolchain in the LINUX_MULTIARCH_ROOT environment variable.")
endif()
# Clang target triple
set(TARGET_TRIPLE x86_64-unknown-linux-gnu)
# Clean path separators
file(TO_CMAKE_PATH $ENV{LINUX_MULTIARCH_ROOT}/${TARGET_TRIPLE} TOOLCHAIN_ROOT)
list(APPEND CMAKE_PROGRAM_PATH ${TOOLCHAIN_ROOT}/bin)
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR x86_64)
# specify the cross compiler
set(CMAKE_C_COMPILER_TARGET ${TARGET_TRIPLE})
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER_TARGET ${TARGET_TRIPLE})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_ASM_COMPILER_TARGET ${TARGET_TRIPLE})
set(CMAKE_ASM_COMPILER clang)
# C/C++ toolchain
set(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN ${TOOLCHAIN_ROOT})
set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN ${TOOLCHAIN_ROOT})
# This must be set or compiler checks fail when linking
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_ROOT})
set(CMAKE_SYSROOT ${TOOLCHAIN_ROOT})
This seems to detect the compilers OK, but fails when CMake attempts to find other packages that I require. The Threads
package is unable to be found using this toolchain, even though I can verify that libpthread.so/.a
exist on disk.
My suspicion is that the Threads
package is not found because CMake is expecting to find a package description file, rather than just the libraries and headers. As far as I know this is not provided with the Clang toolchain distribution. I don’t know where the toolchain specifically comes from - it could even be one that Epic maintains and builds in-house specifically for UE - but I would have thought that libraries could be found within this toolchain in a similar way to how I’d assume CMake would find Clang-related packages in existing toolchains.
Is there anything I can do here to properly locate the required packages, outside of writing package scripts myself?