Cmake cross compile from OSX to Raspberry pi with OpenCV

Cross compile means that you write, compile and build code on once machine, while run the program on another machine even if its OS and CPU architecture are different.

Why cross compile for RPi?

  1. RPi is too slow to build large project. Makes continuously testing and debugging impossible.
  2. Memory and virtual memory exhausted. Like this. It could easily happens when you enable neon optimisation in gcc/g++.

You have no choice but to cross compile. So let's start it!

Environment

Compile Machine
OSX: 10.11.6
cmake: 3.7.2
cross compiler: linaro-arm-linux-gnueabihf-raspbian

** Target Device **
RPi: Model 3 B, Jessie Rasparian
OpenCV v2.4.9 (installed via apt-get)

On RPi

First of all, get your pi 3 wifi working by editing /etc/network/interfaces

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid "YOUR_SSID"
wpa-psk "YOUR_PASSWORD"
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Remember to comment out wpa-conf ..., that is just wasting your time, then sudo reboot. By the way, sudo iwlist wlan0 scan | grep ESSID is a nice tool.

Install everything you need.

apt-get install build-essential git libopencv-dev # and other libs you need

On Compile Machine

Install cmake and linaro g++ compiler on your OSX, the default installation location is /usr/local/linaro/arm-linux-gnueabihf-raspbian/, we need to tell cmake to use this compiler later.

Then copy everything of /lib, /usr, /opt from RPi to your compile machine. You could just copy from pi's SD card or use, or the other way, use rsync. I personally recommend to use rsync, because you can't copy from SD card every time when your pi get's updated or upgrade or installed something new, that's pretty annoying.

So let's copy pi's environment to your OSX. Suppose you can ssh to your pi via ssh pi3

mkdir ~/pi3 && cd ~/pi3
mkdir root && cd root # put your pi files here
rsync -rl pi3:/lib . 
rsync -rl pi3:/usr .
rsync -rl pi3:/opt .

Note that rsync takes long time when first time execute.

Now you have the nessassary environment that compiler needs to build your code. Not that you may want to sync every time your pi gets updated or upgraded.

Before you can cross compile, you need a toolchain file to tell cmake to switch build environment.

In ~/pi3/Toolchain-RaspberryPi.cmake

SET(CMAKE_SYSTEM_NAME Linux) # important for cross compile
SET(CMAKE_SYSTEM_VERSION 1) # not so important

SET(TOOLROOT /usr/local/linaro/arm-linux-gnueabihf-raspbian)
SET(PIROOT $ENV{HOME}/work/pi3/root)

# Specify the cross compiler
SET(CMAKE_C_COMPILER   ${TOOLROOT}/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-g++)


# Where is the target environment
SET(CMAKE_FIND_ROOT_PATH ${PIROOT})

# Search for programs only in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Search for libraries and headers only in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

The toolchain file is per environment so you don't need to have a copy of it in every project.

So far we copied pi3 environment to our OSX, create the toolchain file and setup proper flags in it. Now we'd like to start compile and build our codes.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project( TestProject )

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "Using C++11")

include_directories( 
    ${PIROOT}/usr/include/arm-linux-gnueabihf
    ${PIROOT}/usr/include
    ${PIROOT}/usr/local/include
    ${PIROOT}/opt/vc/include 
    ${PIROOT}/opt/vc/include/interface/vcos/pthreads 
    ${PIROOT}/opt/vc/include/interface/vmcs_host/linux 
)
link_directories( 
    ${PIROOT}/lib/arm-linux-gnueabihf 
    ${PIROOT}/lib
    ${PIROOT}/usr/lib/arm-linux-gnueabihf 
    ${PIROOT}/usr/lib
    ${PIROOT}/usr/local/lib
    ${PIROOT}/opt/vc/lib 
)

find_package(OpenCV REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

# link libs after add_executable
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} )

Build

mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=~/pi3/Toolchain-RaspberryPi.cmake .. && make -j

You can use make VERBOSE=1 for debug.

Troubleshooting

If linker error happens on libc.so or libpthread.so. Edit them by delete the absolute path.

Note that it's not the ones in your OSX system directory, but the ones that you've copied from RPi. Reference.

For example, open ~/pi3/root/usr/lib/arm-linux-gnueabihf/libc.so with text editor on your OSX

OUTPUT_FORMAT(elf32-littlearm)
GROUP ( libc.so.6 libc_nonshared.a  AS_NEEDED ( ld-linux-armhf.so.3 ) )

for libpthread.so

OUTPUT_FORMAT(elf32-littlearm)
GROUP ( libpthread.so.0 libpthread_nonshared.a )

You may have to re-edit them every time after you use rsync.

Optional: Use Raspberry Pi Camera in C++

If you want to use pi-cam in your python code, that's as easy as apt-get install, however, if you want to use it in c++, you have to build for yourself, I don't know why there're no pre-built binary out there. Of course you can now cross compile it but I recommend just simply compile on your RPi, because it just take few seconds. You can find the detail steps here.

Again, remember to sync these libs and headers to your OSX after installation.

Useful links that help you learn

cross compile
RPi and OpenCV
ARM
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容