1. 程式人生 > >在Ubuntu環境下用QT5開發樹莓派的GPIO程式(使用wiringPi庫)之一 搭建環境

在Ubuntu環境下用QT5開發樹莓派的GPIO程式(使用wiringPi庫)之一 搭建環境

準備工作:

一個樹莓派,我使用的是B+,裝上最新的Raspbian映象,RASPBIAN JESSIE WITH PIXEL 4.4

https://www.raspberrypi.org/downloads/raspbian/

Ubuntu作業系統,虛擬機器也可以,我使用的是最新的Ubuntu 16.04 LTS 

http://cn.ubuntu.com/download/

都配置好可以連線網際網路之後,我們就可以開始環境搭建了。請參考以下文章:

官方原文:

http://wiki.qt.io/RaspberryPi2EGLFS#A_modern_guide_for_cross-compiling_Qt_5.6_for_HW_accelerated_OpenGL_with_eglfs_on_Raspbian_Jessie_and_setting_up_Qt_Creator

依照步驟便可以搭建好環境,我簡單的說一下搭建的過程以及我改良的一些步驟。

Step by step

2. Unzip and write it to a memory card. Replace ... with the SD card device (check with lsblk or dmesg eg. mmcblk0).

sudo dd if=2015-09-24-raspbian-jessie.img of=... bs=4M

3. Boot it up, run raspi-config, change it to boot to the console instead of X, change the GPU memory to 256 MB, install a bunch of development files (for simplicity we use build-dep, not everything is really needed, but it is easier this way), and prepare our target directory:

sudo raspi-config
sudo nano /etc/apt/sources.list
and uncomment the deb-src line.

以上是安裝raspbian映象,以及更改raspi-config樹莓派的配置為“啟動到命令列介面”和“GPU記憶體為256MB,然後更改使用更快的更新源(可不管)。

下面這些是在樹莓派上完成的!

sudo apt-get update
sudo apt-get build-dep qt4-x11
sudo apt-get build-dep libqt5gui5
sudo apt-get install libudev-dev libinput-dev libts-dev libxcb-xinerama0-dev
 
sudo mkdir /usr/local/qt5pi
sudo chown pi:pi /usr/local/qt5pi

安裝一些必須的檔案。

下面的步驟就是在Ubuntu環境下完成了,但也請確保你的Ubuntu是可以訪問樹莓派的。

4. Back on the host PC, create our working directory and get a toolchain: 

mkdir ~/raspi
cd ~/raspi
git clone https://github.com/raspberrypi/tools

把樹莓派上的/lib,/usr/include,/usr/lib,/opt/vc裡的檔案同步到Ubuntu裡來。命令裡的IP替換成你那裡樹莓派的IP地址。

5. Create a sysroot. Using rsync we can properly keep things synchronized in the future as well. Replace IP with the address of the Pi.

mkdir sysroot sysroot/usr sysroot/opt
rsync -avz [email protected]<IP>:/lib sysroot例如我的是:rsync -avz [email protected]:/lib sysroot
rsync -avz [email protected]<IP>:/usr/include sysroot/usr
rsync -avz [email protected]<IP>:/usr/lib sysroot/usr
rsync -avz [email protected]<IP>:/opt/vc sysroot/opt

***調整替換掉舊的fixQualifiedLibraryPaths***

6. Adjust symlinks to be relative. Instead of the old fixQualifiedLibraryPaths get a script that works:

wget https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py
chmod +x sysroot-relativelinks.py
./sysroot-relativelinks.py sysroot

到了這步我不推薦用這裡的方法,GIT太慢,而且這條命令只下載了qt基本元件,這麼做的話不利於後續模組的安裝(模組間的依賴關係會把人搞瘋的)。所以,直接去下載個完全版。

http://download.qt.io/official_releases/qt/5.6/5.6.1/single/

下好了解壓到目錄裡面。


7. Get qtbase 5.6 and configure Qt. The target directory is /usr/local/qt5pi on the Pi, the host tools like qmake will go to ~/raspi/qt5, while make install will target ~/raspi/qt5pi (this is what we will sync to the device). If using the old Pi instead of the Pi 2, change -device to linux-rasp-pi-g++. Don't forget to adjust the path to your ~/raspi directory if you changed that. For some reason the ~/ in the paths may not work, if this the case just use full paths.

git clone git://code.qt.io/qt/qtbase.git -b 5.6
cd qtbase<------我的這裡就改成cd qtsrc
./configure -release -opengl es2 -device linux-rasp-pi2-g++ \
-device-option CROSS_COMPILE=~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- \
-sysroot ~/raspi/sysroot -opensource -confirm-license -make libs \
-prefix /usr/local/qt5pi -extprefix ~/raspi/qt5pi -hostprefix ~/raspi/qt5 -v
 
make
make install

經過漫長的等待之後,如果沒有報錯的話,恭喜你,你的環境已經搭建好了。

然後把支援檔案同步回樹莓派裡。

8. Deploy to the device. We simply sync everything from ~/raspi/qt5pi to the prefix we configured above.

rsync -avz qt5pi [email protected]:/usr/local

這裡是編譯一個範例的方法。重點劃線的三條命令我們以後會經常用。

9. Build an example:

cd qtbase/examples/opengl/qopenglwidget
~/raspi/qt5/bin/qmake
make
scp qopenglwidget [email protected]:/home/pi

下面回到樹莓派上(或者SSH過去)

寫一些配置,連結

10. On the device, let the linker find the Qt libs:

echo /usr/local/qt5pi/lib | sudo tee /etc/ld.so.conf.d/qt5pi.conf
sudo ldconfig

11. Still on the device, fix the EGL/GLES library nonsense:

The device may have the Mesa version of libEGL and libGLESv2 in /usr/lib/arm-linux-gnueabihf, resulting Qt apps picking these instead of the real thing from /opt/vc/lib. This may be fine for X11 desktop apps not caring about OpenGL performance but is totally useless for windowing system-less, fullscreen embedded apps.

sudo rm /usr/lib/arm-linux-gnueabihf/libEGL.so.1.0.0 /usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.0.0
sudo ln -s /opt/vc/lib/libEGL.so /usr/lib/arm-linux-gnueabihf/libEGL.so.1.0.0
sudo ln -s /opt/vc/lib/libGLESv2.so /usr/lib/arm-linux-gnueabihf/libGLESv2.so.2.0.0

In case you are using qtwebengine please make sure to add missing symbolic links.

sudo ln -s /opt/vc/lib/libEGL.so /opt/vc/lib/libEGL.so.1
sudo ln -s /opt/vc/lib/libGLESv2.so /opt/vc/lib/libGLESv2.so.2

You may want to save the originals somewhere, just in case.

12. Run qopenglwidget we deployed to /home/pi. At this point it should just work at fullscreen with 60 FPS and mouse, keyboard, and possibly touch support.

結束環境搭建。

下面是安裝其他模組的辦法,如果你按照我的方法下載的完全包編譯完的話,這裡的內容可以無視。

13. Build other Qt modules as desired, the steps are the same always:

~/raspi/qt5/bin/qmake -r
make
make install
then deploy the new files by running
rsync -avz qt5pi [email protected]:/usr/local
in ~/raspi

小結:

其實環境的搭建還是很簡單的,容易出錯的地方可能就只有配置MAKEFILE檔案那裡,細細檢查細細思考,就沒有什麼問題

./configure -release -opengl es2 -device linux-rasp-pi2-g++ \
-device-option CROSS_COMPILE=~/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf- \      
-sysroot ~/raspi/sysroot -opensource -confirm-license -make libs \
-prefix /usr/local/qt5pi -extprefix ~/raspi/qt5pi -hostprefix ~/raspi/qt5 -v
這裡的路徑容易錯,實在不行換成你的絕對路徑。