Project

General

Profile

Cross-compiling NDN projects for Raspberry Pi » History » Version 47

Junxiao Shi, 10/08/2019 08:45 AM

1 47 Junxiao Shi
# NDN Packages for Raspberry Pi
2
3
For Raspberry Pi 2 and newer, install [Ubuntu](https://wiki.ubuntu.com/ARM/RaspberryPi) and then install from [named-data PPA](https://launchpad.net/~named-data/+archive/ubuntu/ppa).
4
Original content of this page is retained for historical purpose.
5
6
---
7
8
9 1 Wentao Shang
Cross-compiling NDN projects for Raspberry Pi
10
=============================================
11
12 2 Wentao Shang
Note: before reading this document, you should already be familiar with the basic concepts of compiling and linking (especially the linking part). If not, you may be interested in reading this great book: [Linkers and Loaders](http://www.amazon.com/Linkers-Kaufmann-Software-Engineering-Programming/dp/1558604960/ref=sr_1_1?ie=UTF8&qid=1394130356&sr=8-1&keywords=linker+and+loader)
13 3 Wentao Shang
14 10 Wentao Shang
Basic idea
15 7 Wentao Shang
----------
16 1 Wentao Shang
17 7 Wentao Shang
Remember to compile a C/C++ project, we need the source code for the project, the header files for the included libraries, the binary objects of the libraries, and the compiler tools (gcc, as, ld, etc.). The combination of the last three things together is referred to as a _building environment_. Cross-compiling is no different. To cross compile a project, we first need to setup the building environment and then build the source code in that environment.
18
19
Difference between "native compiling" and "cross compiling"
20 9 Wentao Shang
--------
21 7 Wentao Shang
22 14 Wentao Shang
The biggest difference is that for native compiling, you build the binaries that will run on the same platform where you build them. For cross compiling, however, you build the binaries on one platform (called _build_ platform) and run them on another platform (called _host_ or _target_ platform). The platforms may differ in the operating systems (Windows vs. Linux) and/or the CPU architectures (x86_64 vs. arm32).
23 8 Wentao Shang
24 10 Wentao Shang
Raspberry Pi platform information
25 1 Wentao Shang
--------
26 10 Wentao Shang
27
Raspberry Pi runs on *ARMv6* CPU, which is a 32bit chip with hardware float-point support (abbreviated as *armhf*). There are many operating systems available. The one we are going to use is called *Raspbian*, which is a port of the Debian "wheezy" Linux distribution.
28
29 15 Wentao Shang
Creating compiling toolchain for Raspberry Pi
30 10 Wentao Shang
--------
31 1 Wentao Shang
32 46 Wentao Shang
To prepare a building environment, we need to get the gcc/g++ compiler toolchain that will generate binaries for the armhf platform. Raspbian already provided a set of compiling tools on their official [github](https://github.com/raspberrypi/tools), which works for both 32bit and 64bit Linux. If you decide to use the official toolchain, you may skip the rest of this section.
33 15 Wentao Shang
34
The tool we are going to use to build our gcc is [ct-ng](http://crosstool-ng.org/). It is designed to run on Linux but can be adjusted via some hacks to run on MacOSX. It is pretty easy to use and you may find this [article](http://www.kitware.com/blog/home/post/426) very helpful when building your own toolchain.
35
36
Tip: you may use the configuration file from Raspbian [github](https://github.com/raspberrypi/tools/blob/master/configs/bcm2708hardfp-ct-ng.config), which will load the "official" configurations for the platform.
37 16 Wentao Shang
38 40 Wentao Shang
We have a toolchain package available for download. It is compiled on Ubuntu 12.04 64bit platform and should also work for later versions of Ubuntu 64bit. The download link is http://irl.cs.ucla.edu/~wentao/pi/pi-tools.tar.gz
39
40 16 Wentao Shang
Getting libraries ready
41
--------
42 17 Wentao Shang
43 18 Wentao Shang
After we have the toolchain, the next step is to gather the libraries, including the header files (_.h_ files) and the binaries (_.a, .so_ files). The libraries used by the NDN projects include *openssl, Boost, sqlite3, crypto++*. Those libraries may recursively depend on other libraries and it will be a big headache to compile all of them from source code and resolve the dependencies manually. Fortunately, Raspbian has a public package repo containing the binaries for most packages available on Debian. So the easy walk-around is to _install_ those libraries directly on Raspberry Pi using _apt-get_ and then copy the relevant files down to our build machine.
44 19 Wentao Shang
45 21 Wentao Shang
On Raspbian, all the header files are in the */usr/include* folder, while the binary objects for the libraries are scattered in many places, such as */usr/lib, /lib*, etc. A simple way to find the path of a library is to run the following command:
46 19 Wentao Shang
47
    ldconfig -p | grep _library_name_
48
49 20 Wentao Shang
*ldconfig -p* will show all the dynamic linking libraries currently available on the system and their locations in the file system.
50 22 Wentao Shang
51
You may copy all the binaries into the same folder. For example, on the build machine you may have the folder _~/pi/_ and inside that folder there are two sub-folders _./include_ and _./lib_. You can copy all the header files into the include folder and the binaries into the lib folder.
52
53 33 Wentao Shang
Here is the list of library files that you need to get in order to compile the NDN projects. You MAY need other library files for your own project.
54
55
    .
56
    ├── include
57 34 Wentao Shang
    │   ├── boost/
58
    │   ├── cryptopp/
59 33 Wentao Shang
    │   ├── expat_config.h
60
    │   ├── expat_external.h
61
    │   ├── expat.h
62 34 Wentao Shang
    │   ├── openssl/
63
    │   ├── pcap/
64 33 Wentao Shang
    │   ├── pcap-bpf.h
65
    │   ├── pcap.h
66
    │   ├── pcap-namedb.h
67
    │   ├── sqlite3ext.h
68
    │   └── sqlite3.h
69
    └── lib
70
        ├── ld-linux-armhf.so.3
71
        ├── libboost_chrono.so
72
        ├── libboost_date_time.so
73
        ├── libboost_filesystem.so
74
        ├── libboost_iostreams.so
75
        ├── libboost_program_options.so
76 42 Wentao Shang
        ├── libboost_random.so
77 33 Wentao Shang
        ├── libboost_regex.so
78
        ├── libboost_system.so
79
        ├── libbz2.so.1.0
80
        ├── libcryptopp.so -> libcrypto++.so
81
        ├── libcrypto.so
82
        ├── libcrypto++.so
83
        ├── libexpat.so
84
        ├── libicudata.so.48
85
        ├── libicui18n.so.48
86
        ├── libicuuc.so.48
87
        ├── libpcap.so
88
        ├── libsqlite3.so
89
        ├── libssl.so
90
        ├── libz.so.1
91 1 Wentao Shang
        └── pkgconfig
92 41 Wentao Shang
93
We have packaged the minimum library files into a package and made it available for download. Note that the Boost library contained in this package is not complete: it only includes the minimum required libs for ndn-cxx and NFD project. The download link is http://irl.cs.ucla.edu/~wentao/pi/pi-env.tar.gz
94 33 Wentao Shang
95 43 Wentao Shang
Note: later versions of ndn-cxx also added dependency on libboost\_random, which is not included in the pi-env package. The .so file can be downloaded at http://irl.cs.ucla.edu/~wentao/pi/libboost_random.so
96
97 22 Wentao Shang
Building the source code
98
--------
99 23 Wentao Shang
100 24 Wentao Shang
The final step is to compile the projects using the environment we created. The basic idea is to call the cross toolchain and link against the cross-compiled libraries. To do that, we need to export a set of shell variables that are used by the *make* command. For example, we can export the following variables in the shell:
101
102
    export AS=/path/to/your/toolchain/as
103
    export LD=/path/to/your/toolchain/ld
104
    export CC=/path/to/your/toolchain/cc
105
    export CXX=/path/to/your/toolchain/c++
106
107 28 Wentao Shang
This will tell *make* to use the toolchain we specify instead of the default toolchain.
108 25 Wentao Shang
109 29 Wentao Shang
In order to point the toolchain to the correct location to search libraries, we also need to export *CFLAGS/CPPFLAGS* and *LDFLAGS* variables, which will be provided to *gcc* as options:
110 1 Wentao Shang
111 26 Wentao Shang
    export CFLAGS="-I/path/to/your/include/folder -L/path/to/your/lib/folder -Wl,-rpath=/path/to/your/lib/folder"
112
    export LDFLAGS="-L/path/to/your/lib/folder -Wl,-rpath=/path/to/your/lib/folder"
113
114 28 Wentao Shang
Note that we repeat the *LDFLAGS* in the *CFLAGS*. This is because some Makefile script may combine the compiling and linking steps together and use only *CFLAGS*. (This actually happens when building the NDNx project.)
115 26 Wentao Shang
116 28 Wentao Shang
Another important option is the _rpath_ option. This specifies where the linker will search for recursive dependencies.
117 35 Wentao Shang
118 38 Wentao Shang
Here is a sample script for configuring & compiling NFD. It can be ported to compile other *waf* based projects as well.
119 35 Wentao Shang
120
    #!/bin/bash
121
122
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
123
124
    arch="arm-bcm2708hardfp-linux-gnueabi"
125
126
    export AR="${arch}-ar"
127
    export AS="${arch}-as"
128
    export LD="${arch}-ld"
129
    export CC="${arch}-cc"
130
    export CXX="${arch}-c++"
131
    export RANLIB="${arch}-ranlib"
132
133
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib"
134 44 Wentao Shang
    export CXXFLAGS="-O2 -g -std=c++11 -I/home/wentao/pi/include -L/home/wentao/pi/lib"
135 35 Wentao Shang
    export LDFLAGS="-Wl,-rpath=/home/wentao/pi/lib -L/home/wentao/pi/lib"
136
    export PKG_CONFIG_PATH=~/pi/lib/pkgconfig
137
138
    ./waf configure --prefix=/home/wentao/pi --boost-includes=/home/wentao/pi/include --boost-libs=/home/wentao/pi/lib
139
140 45 Wentao Shang
    if [ $? = 0 ]
141
    then
142
        ./waf clean
143
        ./waf
144
    fi
145 1 Wentao Shang
146 38 Wentao Shang
After running this script, you may run *./waf install* to copy all the generated binaries into the folder you specified in *--prefix* option at *./waf configure* step. In our case, it will be */home/wentao/pi*. This is helpful if you are cross-compiling your own libraries (e.g., ndn-cxx) that you want to use to further compile other projects.
147 35 Wentao Shang
148 39 Wentao Shang
Another sample script for configuring and compiling ndn-cxx:
149
150
    #!/bin/bash
151
152
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
153
154
    arch="arm-bcm2708hardfp-linux-gnueabi"
155
156
    export AR="${arch}-ar"
157
    export AS="${arch}-as"
158
    export LD="${arch}-ld"
159
    export CC="${arch}-cc"
160
    export CXX="${arch}-c++"
161
    export RANLIB="${arch}-ranlib"
162
163
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib"
164 44 Wentao Shang
    export CXXFLAGS="-O2 -g -std=c++11"
165 39 Wentao Shang
    export LDFLAGS="-Wl,-rpath=/home/wentao/pi/lib"
166
    export PKG_CONFIG_PATH=~/pi/lib/pkgconfig
167
168 1 Wentao Shang
    ./waf configure --prefix=/home/wentao/pi --boost-includes=/home/wentao/pi/include --boost-libs=/home/wentao/pi/lib --with-openssl=/home/wentao/pi --with-cryptopp=/home/wentao/pi
169
170 45 Wentao Shang
    if [ $? = 0 ]
171
    then
172
        ./waf clean
173
        ./waf
174
    fi
175
176
Note: starting from Oct. 30, 2014, ndn-cxx and NFD have switched to adopt C++11 standard. As a result, you need to feed "-std=c++11" flag to gcc during cross-compiling.
177 39 Wentao Shang
178 38 Wentao Shang
Here is the sample script for configuring & compiling NDNx. It can be ported to compile other *automake* based projects as well.
179
180 35 Wentao Shang
    #!/bin/bash
181
182
    export PATH=/home/wentao/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:$PATH
183
184
    arch="arm-bcm2708hardfp-linux-gnueabi"
185
186
    export AR="${arch}-ar"
187
    export AS="${arch}-as"
188
    export LD="${arch}-ld"
189
    export CC="${arch}-cc"
190
    export CXX="${arch}-c++"
191
    export RANLIB="${arch}-ranlib"
192
193
    export CFLAGS="-O2 -std=gnu99 -I/home/wentao/pi/include -L/home/wentao/pi/lib -Wl,-rpath=/home/wentao/pi/lib"
194 1 Wentao Shang
    export LDFLAGS="-L/home/wentao/pi/lib -Wl,-rpath=/home/wentao/pi/lib"
195
196 35 Wentao Shang
    ./configure --build="x86_64-linux-gnu" --host="${arch}" --prefix="/home/wentao/pi"
197 38 Wentao Shang
198
    make