C/C++交叉编译各个平台可执行程序

前言

目前主流的平台有:

  • Windows
  • Mac
  • Linux
  • Android
  • IOS

C/C++程序写好后 如何快速在当前平台编译出各个平台的可执行文件

现在比较成熟的方案有:

  • 利用docker容器编译
  • 利用cmake构建交叉编译环境

利用docker容器编译

基于GCC套件, 可实现对Android, Linux, Windows, Web平台的交叉编译, 详细介绍如下:

  • dockcross/base

    Base image for other toolchain images. From Debian Jessie with GCC, make, autotools, CMake, Ninja, Git, and Python.

  • dockcross/android-arm

    The Android NDK standalone toolchain for the arm architecture.

  • dockcross/android-arm64

    The Android NDK standalone toolchain for the arm64 architecture.

  • dockcross/linux-arm64

    Cross compiler for the 64-bit ARM platform on Linux, also known as AArch64.

  • dockcross/linux-armv5

    Linux armv5 cross compiler toolchain for legacy devices like the Parrot AR Drone.

  • dockcross/linux-armv5-musl

    Linux armv5 cross compiler toolchain using musl as base “libc”.

  • dockcross/linux-armv6

    Linux ARMv6 cross compiler toolchain for the Raspberry Pi, etc.

  • dockcross/linux-armv7

    Generic Linux armv7 cross compiler toolchain.

  • dockcross/linux-armv7a

    Toolchain configured for ARMv7-A used in Beaglebone Black single board PC with TI SoC AM3358 on board, Cortex-A8.

  • dockcross/linux-mipsel

    Linux mipsel cross compiler toolchain for little endian MIPS GNU systems.

  • dockcross/linux-mips

    Linux mips cross compiler toolchain for big endian 32-bit hard float MIPS GNU systems.

  • dockcross/linux-s390x

    Linux s390x cross compiler toolchain for S390X GNU systems.

  • dockcross/linux-ppc64le

    Linux PowerPC 64 little endian cross compiler toolchain for the POWER8, etc. Important: Due to Issue #430, automatic build of newer images has been disabled.

  • dockcross/linux-x64

    Linux x86_64 / amd64 compiler. Since the Docker image is natively x86_64, this is not actually a cross compiler.

  • dockcross/linux-x86

    Linux i686 cross compiler.

  • dockcross/manylinux2014-x64

    Docker manylinux2014 image for building Linux x86_64 / amd64 Python wheel packages. It includes Python 3.5, 3.6, 3.7, 3.8, and 3.9. Also has support for the dockcross script, and it has installations of CMake, Ninja, and scikit-build. For CMake, it sets MANYLINUX2014 to “TRUE” in the toolchain.

  • dockcross/manylinux2014-x86

    Docker manylinux2014 image for building Linux i686 Python wheel packages. It includes Python 3.5, 3.6, 3.7, 3.8, and 3.9. Also has support for the dockcross script, and it has installations of CMake, Ninja, and scikit-build. For CMake, it sets MANYLINUX2014 to “TRUE” in the toolchain.

  • dockcross/manylinux2014-aarch64

    Docker manylinux2014 image for building Linux aarch64 / arm64 Python wheel packages. It includes Python 3.5, 3.6, 3.7, 3.8, and 3.9. Also has support for the dockcross script, and it has installations of CMake, Ninja, and scikit-build. For CMake, it sets MANYLINUX2014 to “TRUE” in the toolchain.

  • dockcross/manylinux2010-x64

    Docker manylinux2010 image for building Linux x86_64 / amd64 Python wheel packages. It includes Python 2.7, 3.4, 3.5, 3.6, 3.7 and 3.8. Also has support for the dockcross script, and it has installations of CMake, Ninja, and scikit-build. For CMake, it sets MANYLINUX2010 to “TRUE” in the toolchain.

  • dockcross/manylinux2010-x86

    Docker manylinux2010 image for building Linux i686 Python wheel packages. It includes Python 2.7, 3.4, 3.5, 3.6, 3.7 and 3.8. Also has support for the dockcross script, and it has installations of CMake, Ninja, and scikit-build. For CMake, it sets MANYLINUX2010 to “TRUE” in the toolchain.

  • dockcross/manylinux1-x64

    Docker manylinux1 image for building Linux x86_64 / amd64 Python wheel packages. It includes Python 2.7, 3.4, 3.5, 3.6, 3.7 and 3.8. Also has support for the dockcross script, and it has installations of CMake, Ninja, and scikit-build. For CMake, it sets MANYLINUX1 to “TRUE” in the toolchain.

  • dockcross/manylinux1-x86

    Docker manylinux1 image for building Linux i686 Python wheel packages. It includes Python 2.7, 3.4, 3.5, 3.6, 3.7 and 3.8. Also has support for the dockcross script, and it has installations of CMake, Ninja, and [ scikit-build](http:// scikit-build.org/). For CMake, it sets MANYLINUX1 to “TRUE” in the toolchain.

  • dockcross/web-wasm

    The Emscripten WebAssembly/asm.js/JavaScript cross compiler.

  • dockcross/windows-static-x64

    64-bit Windows cross-compiler based on MXE/MinGW-w64 with win32 threads and static linking.

  • dockcross/windows-static-x64-posix

    64-bit Windows cross-compiler based on MXE/MinGW-w64 with posix threads and static linking.

  • dockcross/windows-static-x86

    32-bit Windows cross-compiler based on MXE/MinGW-w64 with win32 threads and static linking.

  • dockcross/windows-shared-x64

    64-bit Windows cross-compiler based on MXE/MinGW-w64 with win32 threads and dynamic linking.

  • dockcross/windows-shared-x64-posix

    64-bit Windows cross-compiler based on MXE/MinGW-w64 with posix threads and dynamic linking.

  • dockcross/windows-shared-x86

    32-bit Windows cross-compiler based on MXE/MinGW-w64 with win32 threads and dynamic linking.

具体使用可参见Github

使用cmake构建交叉编译环境

首先我们需要下载cmake构建工具

然后下载用于交叉编译的套件

Mac平台为例, 如果我需要编译各个平台的可执行文件, 那么首先需要下载能在Mac平台运行的目标平台编译包, 比如:

  • android平台:下载 Mac平台可运行的NDK (基于clang)

  • ios平台: 下载iphoneos SDK, xcode自带 我们只需下载xcode即可 (基于clang)

  • windows平台:下载MinGW 或者 MXE/MinGW-w64 (基于GCC)

  • Linux平台:如 arm/x86-Linux-gnueabi套件(基于GCC)

本文为作者原创转载时请注明出处 谢谢

乱码三千 – 点滴积累 ,欢迎来到乱码三千技术博客站

0%