Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

cmake - Cross compiling for x64_arm with MSVC tools on Windows

I am using MSVC command line tools to try to compile a simple c++ based program for arm/arm64 on a x64 machine. Even after using the toolchain file, the compiler definition is not getting updated and the default compiler for host is being used. Please read further for more information on the issue.

Testcase:

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello world!" << endl;
}

CMakelists.txt:

cmake_minimum_required(VERSION 3.14.3)
project(hello C CXX ASM)
enable_language(ASM)
add_executable(hello hello.cpp)

Toolchain file:

cmake_minimum_required(VERSION 3.14.3)

set(CMAKE_CROSSCOMPILING 1)
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR ARM)
set(CMAKE_SYSTEM_VERSION 10.0)

set(CMAKE_C_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe")
set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe")
set(CMAKE_ASM_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe")

The command I used to build this:

cmake.exe -S C:hello -B C:hellooutput_dir.cmake -DCMAKE_TOOLCHAIN_FILE=C:helloToolchain.cmake

With the above configuration, I am able to build a x64 based application but not a cross compiled ARM based one. From console logs, I see that the c and c++ compiler definition is not overwritten but ASM compiler is overwritten using the toolchain file.

-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.
-- The C compiler identification is MSVC 19.28.29336.0
-- The CXX compiler identification is MSVC 19.28.29336.0
-- The ASM compiler identification is MSVC
-- Found assembler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/hello/output_dir.cmake 

I am not sure what is missing in this configuration. Can anyone please help from their experience?

question from:https://stackoverflow.com/questions/66063056/cross-compiling-for-x64-arm-with-msvc-tools-on-windows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks for your answers.

Using set(CMAKE_GENERATOR_PLATFORM ARM64 CACHE INTERNAL "") in my toolchain file worked for me, following are the logs picking correct compiler:

> -- Building for: Visual Studio 16 2019
> -- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.
> -- The C compiler identification is MSVC 19.28.29336.0
> -- The CXX compiler identification is MSVC 19.28.29336.0
> -- The ASM compiler identification is MSVC
> -- Found assembler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe
> -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual
> Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm64/cl.exe
> -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual
> Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm64/cl.exe
> - works
> -- Detecting C compiler ABI info
> -- Detecting C compiler ABI info - done
> -- Detecting C compile features
> -- Detecting C compile features - done
> -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual
> Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm64/cl.exe
> -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual
> Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm64/cl.exe
> - works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Detecting CXX compile features
> -- Detecting CXX compile features - done
> -- Configuring done
> -- Generating done
> -- Build files have been written to: C:/hello/output_dir.cmake

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...