Cross-compiling under Visual Studio
Hi, I am trying to cross-compile CMake project in Visual Studio using the following toolchain file:
cmake_minimum_required (VERSION 3.12)
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_VERSION 10.0)
set(CMAKE_SYSTEM_PROCESSOR AMD64)
set(CMAKE_C_COMPILER "${MY_TOOLCHAIN}/path/to/c_compiler.exe")
set(CMAKE_CXX_COMPILER "${MY_TOOLCHAIN}/path/to/cxx_compiler.exe")
foreach(TARGET EXE SHARED STATIC MODULE)
set(CMAKE_${TARGET}_LINKER_FLAGS_INIT "/LIBPATH:\"${MY_TOOLCHAIN}/path/to/library_directories\"")
string(REPLACE ";" " " CMAKE_${TARGET}_LINKER_FLAGS_INIT "${CMAKE_${TARGET}_LINKER_FLAGS_INIT}")
endforeach()
foreach(LANG C CXX)
set(CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES "${MY_TOOLCHAIN}/path/to/include_directories")
endforeach()
set(CMAKE_FIND_ROOT_PATH "${MY_TOOLCHAIN}")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Unfortunately I've encountered an issue:
- Visual Studio generator doesn't respect variables CMAKE_<LANG>_COMPILER.
Essentially, Visual Studio uses compilers installed on the host machine
instead compilers located in the toolchain:
C:\Users\mtezych\Desktop\cmake-app\build>cmake -DCMAKE_TOOLCHAIN_FILE=C:\Users\mtezych\Desktop\MyToolchain.cmake -G "Visual Studio 15 2017 Win64" ..
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx86/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx86/x64/cl.exe -- works
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx86/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx86/x64/cl.exe -- works
C:\Users\mtezych\Desktop\cmake-app\build>cmake --build .
ClCompile:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.14.26428\bin\HostX86\x64\CL.exe ... /c "C:\Users\mtezych\Desktop\cmake-app\app.cpp"
Link:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.14.26428\bin\HostX86\x64\link.exe ... /OUT:"C:\Users\mtezych\Desktop\cmake-app\build\Debug\app.exe" app.dir\Debug\app.obj
I've managed to create W/A for this issiue by using CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES variable, like so:
set(CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES "${MY_TOOLCHAIN}/path/to/c/and/cxx/compilers")
However, to me it looks like there is something wrong with the way
Visual Studio generator handles cross-compilers defined by toolchain file.
What is the recommended way of defining C and C++ compilers for cross-compiling with Visual Studio?
BTW, when I'm using CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES from CMake 3.12,
CMake still "uses" C and C++ compilers from the host machine
(this can be observed how CMake checks if C and C++ compilers work),
but the compilation itself uses compilers from the toolchain:
C:\Users\mtezych\Desktop\cmake-app\build>cmake -DCMAKE_TOOLCHAIN_FILE=C:\Users\mtezych\Desktop\MyToolchain.cmake -G "Visual Studio 15 2017 Win64" ..
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx86/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx86/x64/cl.exe -- works
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx86/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.14.26428/bin/Hostx86/x64/cl.exe -- works
C:\Users\mtezych\Desktop\cmake-app\build>cmake --build .
ClCompile:
C:/my_toolchain/path/to/cxx_compiler.exe ... /c "C:\Users\mtezych\Desktop\cmake-app\app.cpp"
Link:
C:/my_toolchain/path/to/linker.exe ... /OUT:"C:\Users\mtezych\Desktop\cmake-app\build\Debug\app.exe" app.dir\Debug\app.obj