您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页Cmake cross compile from OSX to

Cmake cross compile from OSX to

来源:二三娱乐

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 . 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

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.

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++

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

Useful links that help you learn

  • Must Read:
  • 必讀:
  • Recommended:
  • Recommended:
cross compile
  • 中文:
RPi and OpenCV
ARM

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务