QF10
QF10

Reputation: 11

How to cross-compile with CMake

I currently have a Visual Studio Code project, I'm running my main.cpp with CMake and everything works fine. I can also run my program from a command terminal under Ubuntu. I also managed to cross-compile a simple program but the problem is that my main.cpp is in a big project. My question is, is there a simple way to cross-compile my main.cpp for an ARM architecture (I am on x64-64). I have heard about toolchains but the examples don't really correspond to my case so I write this question. And i dont know how to edit my CMakeLists.txt in order to do this. Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.0)
project(hash VERSION 0.1.0)



FILE(GLOB srcSources src/*.cpp)


find_package(Qt5Core REQUIRED)


add_executable(hash
${srcSources}
)

target_link_libraries(hash Qt5::Core)



target_include_directories(hash PUBLIC
"${PROJECT_SOURCE_DIR}/include"

)

Thank you for your help

Upvotes: 0

Views: 17046

Answers (1)

mo_al_
mo_al_

Reputation: 704

You can try creating a CMake toolchain file like, you can call it my_toolchain.cmake:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR ARM)

set(TOOLCHAIN_PREFIX arm-linux-gnueabi-)
execute_process(
  COMMAND which ${TOOLCHAIN_PREFIX}gcc
  OUTPUT_VARIABLE BINUTILS_PATH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

get_filename_component(ARM_TOOLCHAIN_DIR ${BINUTILS_PATH} DIRECTORY)

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)

set(CMAKE_SYSROOT ${ARM_TOOLCHAIN_DIR}/../arm-linux-gnueabi)
set(CMAKE_FIND_ROOT_PATH ${BINUTILS_PATH})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Then use it to invoke CMake:

cmake -DCMAKE_TOOLCHAIN_FILE="my_toolchain.cmake" -S. -Bbin

Of course you need Qt-core to be installed in the /lib path in your cross-compiler's sysroot

Upvotes: 5

Related Questions