Project

General

Profile

Cross compile Wt on Raspberry Pi » History » Version 5

Wim Dumon, 11/15/2022 02:15 PM

1 5 Wim Dumon
Cross compile Wt on Raspberry Pi
2
================================
3 1 Wim Dumon
4
While the Raspberry Pi looks is a complete computer with a full debian distribution, it has a slow flash disk, limited memory, and CPU power under it's hood. Compiling Wt on the device itself is not recommended unless you have a lot of time. The method described here allowed us to cross compile Wt for Raspbian GNU/Linux 7. This guide assumes that Raspbian is installed on your raspberry pi.
5
6 5 Wim Dumon
Note that wt packages are available on the raspbian, so you don't even have to cross-compile Wt is you're ok with the version of Wt included in your raspbian distribution. Just apt-get install it: `apt-get install witty witty-dev witty-doc witty-dbg witty-examples`
7 1 Wim Dumon
8
For embedded deployment, we highly recommend to cross-compile Wt. Wt supports cross-compilation, and we were successful in cross-compiling Wt on the Raspberry Pi using the method described below.
9
10
The general idea is to install a cross-compiler on your host, and the development libraries on the raspberry. Using sshfs, we will compile on the host, using the cross compiler on the host, and the header files on the raspberry pi. The resulting executables can then be copied to the raspberry pi and executed there.
11
12
The instructions below can be used to cross-compile anything that uses cmake, not just Wt. It's also recommended to cross-compile your own applications, you'll notice that it's much faster than compiling on the device itself.
13
14 5 Wim Dumon
Install prerequisites on the raspberry pi
15
-----------------------------------------
16 1 Wim Dumon
17
Log in on your raspberry pi and install the desired dependencies. To build Wt, boost is actually the only required dependency:
18
19 5 Wim Dumon
    # log in on the rpi to install the dependency libraries
20
    ssh pi@raspberrypi
21
    sudo apt-get install libboost-all-dev
22 1 Wim Dumon
23 5 Wim Dumon
Install cross compilation tool chain:
24
-------------------------------------
25
26 1 Wim Dumon
Ideally, raspbian should distribute cross-compilers for their distribution, but as far as I know, they don't. Building a correct, working cross compilation from scratch, even with tools like crosstool-ng, is a challenge in itself and many guides on the internet are either out-dated, flawed, or fail to produce the tools. We did not easily succeed to use crosstool-ng to generate a cross-compilation environment for the same gcc and glibc version as installed on our raspberry pi.
27
28
We finally used a pre-built cross compilation tool chain for a slightly different version of gcc and libc:
29
30 5 Wim Dumon
    # make sure that you've logged out from your rpi, execute these commands in on your host computer:
31
    mkdir ~/rpi
32
    git clone https://github.com/raspberrypi/tools.git
33 4 Wim Dumon
34 5 Wim Dumon
Make raspberry pi development files (header files, libraries) available on the host
35
-----------------------------------------------------------------------------------
36 1 Wim Dumon
37
On your cross compilation host, use sshfs to mount your complete raspberry pi:
38
39 5 Wim Dumon
    # make sure that you've logged out from your rpi, execute these commands in on your host computer:
40
    mkdir ~/mnt_rpi
41
    sshfs pi@rspberrypi:/ ~/mnt_rpi/ -o transform_symlinks
42 1 Wim Dumon
43 5 Wim Dumon
The `-o transform_symlinks` is required to transform absolute symlinks on the rpi to relative ones, so that a link on the rpi filesystem to /lib/libabc.so actually still points to the same file, and not to /lib/libabc.so on the host filesystem.
44 1 Wim Dumon
45 5 Wim Dumon
**Imporant note:** on the raspbian distribution we used, we ran into a second form of 'links' to absolute paths that prevented linking to work out of the box (resulting in link errors for libpthread, libc, libc\_nonshared, ...). The files `/usr/lib/arm-linux-gnueabihf/libc.so` and `/usr/lib/arm-linux-gnueabihf/libpthread.so` are actually linker scripts that refer to absolute paths, which are not correct when cross compiling. Our solution was to remove the absolute path from the linker script.
46
47 1 Wim Dumon
For example:
48 5 Wim Dumon
49
    /* GNU ld script
50
       Use the shared library, but some functions are only in
51
       the static library, so try that secondarily.  */
52
    OUTPUT_FORMAT(elf32-littlearm)
53
    GROUP ( /lib/arm-linux-gnueabihf/libc.so.6 /usr/lib/arm-linux-gnueabihf/libc_nonshared.a  AS_NEEDED ( /lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 ) )
54
55
  
56 1 Wim Dumon
becomes:
57
58 5 Wim Dumon
    /* GNU ld script
59
       Use the shared library, but some functions are only in
60
       the static library, so try that secondarily.  */
61
    OUTPUT_FORMAT(elf32-littlearm)
62
    GROUP ( libc.so.6 libc_nonshared.a  AS_NEEDED ( ld-linux-armhf.so.3 ) )
63
64 1 Wim Dumon
This modification solves the problem and is, as far as we know, otherwise harmless.
65
66 5 Wim Dumon
Make a cmake toolchain file for cross compilation
67
-------------------------------------------------
68 1 Wim Dumon
69 5 Wim Dumon
Create the file below, and save it on your host computer as `~/rpi-toolchain.cmake`.
70 1 Wim Dumon
71 5 Wim Dumon
Replace `/home/me` in the file below by your own home directory.
72 1 Wim Dumon
73 5 Wim Dumon
    SET(CMAKE_SYSTEM_NAME Linux)
74
    SET(CMAKE_SYSTEM_VERSION 1)
75 1 Wim Dumon
76 5 Wim Dumon
    SET(CMAKE_C_COMPILER
77
    /home/me/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc)
78 1 Wim Dumon
79 5 Wim Dumon
    SET(CMAKE_CXX_COMPILER
80
    /home/me/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++)
81 1 Wim Dumon
82 5 Wim Dumon
    # where is the target environment - we mounted it using sshfs
83
    SET(CMAKE_FIND_ROOT_PATH /home/me/mnt_rpi)
84 1 Wim Dumon
85 5 Wim Dumon
    # search for programs in the build host directories
86
    SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
87
    # for libraries and headers in the target directories
88
    SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
89
    SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
90 1 Wim Dumon
91 5 Wim Dumon
Cross compile Wt
92
----------------
93 1 Wim Dumon
94
Once the cross compilation environment is set up, compiling Wt is straight forward:
95
96 5 Wim Dumon
    # make sure that you've logged out from your rpi, execute these commands in on your host computer:
97
    # untar wt: tar xvzf wt-x.y.z.tar.gz
98
    cd wt-x.y.z
99
    mkdir build-rpi
100
    cd build-rpi
101
    cmake ../wt -DCMAKE_TOOLCHAIN_FILE=~/rpi_toolchain.cmake -DSHARED_LIBS=OFF
102
    make -j 8
103
    cd examples
104
    make -j 8
105 1 Wim Dumon
106 5 Wim Dumon
  
107
The SHARED\_LIBS=OFF option makes the Wt libraries static. This is not required, but easier for deployment: you can simply copy the example executables to your raspberry pi without having to worry about the shared libraries.
108
109
Run hello world on the Raspberry Pi
110
-----------------------------------
111
112
    # log in on the rpi and execute hello world
113
    scp examples/hello/hello.wt pi@raspberrypi:
114
    ssh pi@raspberrypi
115
    chmod +x hello.wt
116
    ./hello.wt --docroot . --http-address 0.0.0.0 --http-port 8080
117
118
  
119 1 Wim Dumon
Now surf to your raspberrypi (http://raspberrypi:8080/) from your host computer and you should see the hello world application!