Wiki

Clone wiki

x265_git / CrossCompile

Cross Compiling with CMake

There are two possibilities in order to cross-compile x265 using CMake: 1. passing all the options on the command-line (not recommended) 2. using a CMake configuration file

Using the command-line

Assuming you want to go with the 1. approach, simply enter the source folder and execute:

#!sh

$ cmake -Doption1=value1 -Doption2=value2 && make
where the options and values are the same as those found subsequently in the configuration file section.

Since passing all the options on the command-line is tedious, using a CMake configuration file is recommended.

Using a CMake configuration file

Before building, you have to chose the correct CMake configuration file depending on what kind of build you want to produce. The main options are: statically or dynamically linked and 32-bit or 64-bit.

Exemplary build process:

#!sh

$ cd build/linux
$ cat [appropriate build conf]>build.cmake
$ cmake -DCMAKE_TOOLCHAIN_FILE=build.cmake ../../source
$ make
In other words: simply enter your build directory, copy the correct CMake configuration into a build configuration file and execute cmake using that configuration as input.

32-bit dynamically linked

JEEB first reported success in #x265 building a 32-bit dynamically linked binary using the following CMake configuration file:

#!cmake

# this one is important
SET(CMAKE_SYSTEM_NAME Windows)

# specify the cross compiler
SET(CMAKE_C_COMPILER   i686-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
SET(CMAKE_RC_COMPILER i686-w64-mingw32-windres)
SET(CMAKE_ASM_YASM_COMPILER yasm)

In theory, the line defining CMAKE_ASM_YASM_COMPILER should not be required, but we haven't figured out how to make cmake not try and find a cross-compile prefixed yasm executable. Your version of cmake may not require this.

For compiling using msys, see https://bitbucket.org/multicoreware/x265_git/src/master/build/msys/?at=master (NB the -G "Msys makefiles" is for cmake on windows only).

32-bit statically linked

Zyrill set up his Ubuntu build environment to produce statically linked (32-bit and 64-bit) binaries. For the 32-bit compilation, the CMake configuration file has to be amended as follows:

#!cmake

# this one is important
SET(CMAKE_SYSTEM_NAME Windows)

# specify the cross compiler
SET(CMAKE_C_COMPILER   i686-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
SET(CMAKE_RC_COMPILER i686-w64-mingw32-windres)
SET(CMAKE_ASM_YASM_COMPILER yasm)

SET(CMAKE_CXX_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_C_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")

This will result in a binary that runs on windows without the need for any additional libraries. The additional flags -O3 and -s optimise the binary further and strip unneeded (debug-)symbols and other usually dispensable stuff.

64-bit statically linked

In order to obtain 64-bit binaries, the CMake configuration file should read:

#!cmake

# this one is important
SET(CMAKE_SYSTEM_NAME Windows)

# specify the cross compiler
SET(CMAKE_C_COMPILER   x86_64-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
SET(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
SET(CMAKE_ASM_YASM_COMPILER yasm)

SET(CMAKE_CXX_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_C_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")
SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "-static-libgcc -static-libstdc++ -static -O3 -s")

Updated