CmakeGccm32: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Remove leading space rectangles from preformatted blocks)
(Replace content with link to new CMake community wiki)
Line 1: Line 1:
= How to use gcc-multilib to cross compile software for Linux =

Goal: compile a 32bits exe on running amd64 linux system.
This page has moved [ here].
== Install the gcc-multilib package ==
$ sudo apt-get install gcc-multilib
== Write a CMake toolchain file ==
For CMake to be able to crosscompile software, it requires you to write a toolchain file, which tells CMake
some information about the toolchain.
With the examples used above it will look like:
# the name of the target operating system
# which compilers to use for C and C++
# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH  /usr/i486-linux-gnu )
# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
Save this file as Toolchain-gcc-m32.cmake to some location where you will put
all your toolchain files, e.g. $HOME.
As you can see CMAKE_FIND_ROOT_PATH is set to /usr/i486-linux-gnu, which contains the libraries '''and''' headers 
installed with the toolchain.
== Build the software for Linux ==
Let's say you have the classical hello world software with a CMake based buildsystem and want to build this for Linux
using gcc -m32.
#include <stdio.h>
int main()
  printf("Hello world\n");
  return 0;
ADD_EXECUTABLE(hello main.c)
Then run CMake on it to generate the buildfiles, the important point is that you tell it to use the toolchain file you just wrote:
~/src/helloworld/ $ mkdir build
~/src/helloworld/ $ cd build
~/src/helloworld/build/ $ cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-gcc-m32.cmake -DCMAKE_INSTALL_PREFIX=/home/mathieu/gcc-m32-install ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mathieu/src/helloworld/build
~/src/helloworld/build/ $ make
Scanning dependencies of target hello
[100%] Building C object CMakeFiles/hello.dir/main.o
Linking C executable hello
[100%] Built target hello
You can then verify:
$ file hello
hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.8, not stripped
In case you have a more complex application that is using let say zlib, you would need first to install it:
$ apt-cross --arch i386 -i zlib1g-dev
And after compilation, you can check that you are indeed using this zlib and not your system one:
$ ldd ./myapp
... => /usr/i486-linux-gnu/lib/ (0xf7b6b000)

Latest revision as of 15:41, 30 April 2018

The CMake community Wiki has moved to the Kitware GitLab Instance.

This page has moved here.