1. 程式人生 > >樹莓派&qt5交叉編譯

樹莓派&qt5交叉編譯

Rasberrypi&Qt cross compilation

本以為樹莓派這麼受歡迎,大家應該把開發樹莓派中遇到的問題解決的差不多。但遇到一個又一個問題,解決一個又一個,心累啊,最終還是把環境搭建好了。ps:加入到某些樹莓派qq群,本以為有大神能夠幫忙,結果,進去後剛問問題,所謂的大神就說 新手!! 怎麼啟動的程式……,等我把問題說清楚,全都啞巴了!

樹莓派安裝qt5&qtcreator–>pc交叉編譯對應版本的qt原始碼–>配置pc端的qtcreator–>測試

  • 準備

    • 樹莓派2B+
    • 系統是 2016-09-23-raspbian-jessie.img,該版本支援apt-get方式安裝qt,目前是5.3.2版本。
    • 低版本升級或者重新燒製系統(格式化後重新分割槽,徹底清理乾淨sd卡,否則會發生意想不到的結果)
  • 概覽

    • 樹莓派
      1、系統燒製、驅動安裝
      2、樹莓派安裝qt-default(qt5.3.2) 和 qtcreator
    • PC(ubuntu16.04)
      1、QT官方教程:交叉編譯qt5.3.2要和樹莓派版本對應
      2、qtcreator arm工具及配置,及遠端部署
      3、 忽略這一步—————最終,我們將掛載的映象燒到sd卡,啟動系統後,就可以直接使用qt5了。所以編譯的qt5的庫就在掛載的映象中,我們只需把它拷到樹莓派上,再設定環境變數,就應該能用了。qt5庫位置:/mnt/rasp-pi-rootfs/usr/local/qt5pi
    • 問題彙總
      1、make階段的符號連結問題 undefined reference to `__dlopen’等問題的解決方案
      2、檔案不匹配的問題,重新獲取原始碼github或者qt官網
      3、如果哪天qtcreator編譯樹莓派程式時,發生丟失檔案的錯誤。請先確認是否未掛載樹莓派
      sudo mkdir /mnt/rasp-pi-rootfs
      sudo mount -o loop,offset=62914560 2015-05-05-raspbian-wheezy.img /mnt/rasp-pi-rootfs

      4、啟動樹莓派後,不能正常驅動鍵盤、3.5LCD顯示器。格式化SD卡重新分割槽,燒製系統。
  • 樹莓派上安裝qt5
    • 要求安裝的系統是jessie
    • 安裝qt&qtcreator

      sudo apt-get install qt-default//目前是qt5.3.2
      sudo apt-get install qtcreator//安裝完這一步,qt程式的介面才啟動起來
    • 編譯

      • 掛載樹莓派(百度jessie掛載即可//交叉編譯時會使用樹莓派的庫和工具,以後qtcreator程式時,也會用到系統中的檔案)

         sudo mkdir /mnt/rasp-pi-rootfs
        sudo mount -o loop,offset=62914560 2015-05-05-raspbian-wheezy.img /mnt/rasp-pi-rootfs
      • 矯正系統連結和庫路徑(是用的是掛載的樹莓派檔案;檔案位置~/opt,如果更改,請對應)

        cd ~/opt/cross-compile-tools
        sudo ./fixQualifiedLibraryPaths /mnt/rasp-pi-rootfs/ ~/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc
      • 配置make

        cd ~/opt/cross-compile-tools
        sudo ./fixQualifiedLibraryPaths /mnt/rasp-pi-rootfs/ ~/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc
      • make&install

        make -j 4  //這步會發生符號連結問題,見下
        sudo make install  //安裝到掛載的樹莓派系統中。
      • 燒製映象到SD卡(現在不使用該步驟,直接在樹莓派上以apt-get方式安裝qt-default和qtcreator)
        最終,我們將掛載的映象燒到sd卡,啟動系統後,就可以直接使用qt5了。所以編譯的qt5的庫就在掛載的映象中,我們只需把它拷到樹莓派上,再設定環境變數,就應該能用了。qt5庫位置:/mnt/rasp-pi-rootfs/usr/local/qt5pi
    • 注意事項

      • 交叉編譯前最好不要使用過QT,或者重啟機器後交叉編譯,不要啟動qtcreator等
      • 如果不按照官方教程的檔案結構,請將檔案結構對應準確
      • 關於安裝32庫
         sudo apt-get install ia32-libs
         //如果無法定位ia32-libs嘗試安裝下列內容,或者安裝時提示的軟體 lib32z1 lib32ncurses5 lib32bz2-1.0 
  • 遇到的問題及解決方案

    • make階段的符號連結問題
      按照官方教程正確操作後,出現undefined reference to `__dlopen’等問題,顯示內容如下


      qlibrary_unix.cpp:(.text+0x1330): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
      /mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libdl.a(dlopen.o): In function `dlopen':
      (.text+0xc): undefined reference to `__dlopen'
      /mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libdl.a(dlclose.o): In function `dlclose':
      (.text+0x0): undefined reference to `__dlclose'
      /mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libdl.a(dlsym.o): In function `dlsym':
      (.text+0xc): undefined reference to `__dlsym'
      /mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libdl.a(dlerror.o): In function `dlerror':
      (.text+0x0): undefined reference to `__dlerror'
      /mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libm.a(feholdexcpt.o): In function `feholdexcept':
      (.text+0x48): undefined reference to `_dl_hwcap'
      /mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libm.a(fesetenv.o): In function `fesetenv':
      (.text+0x64): undefined reference to `_dl_hwcap'
      collect2: error: ld returned 1 exit status
      Makefile:1215: recipe for target '../../lib/libQt5Core.so.5.5.1' failed
      make[2]: *** [../../lib/libQt5Core.so.5.5.1] Error 1
      make[2]: Leaving directory '/home/liu/bin/opt/qt5/qtbase/src/corelib'
      Makefile:170: recipe for target 'sub-corelib-make_first' failed
      make[1]: *** [sub-corelib-make_first] Error 2
      make[1]: Leaving directory '/home/liu/bin/opt/qt5/qtbase/src'
      Makefile:45: recipe for target 'sub-src-make_first' failed
      make: *** [sub-src-make_first] Error 2

      解決方案
      軟連線不對,重建兩個軟連線,均在掛載的樹莓派映象上。如果已經存在該軟連結,先刪除再建立。

      //最終,我們將掛載的映象燒到sd卡,啟動系統後,就可以直接使用qt5了,所以編譯的qt5的庫就在掛載的映象中,我們只需把它拷到樹莓派上,再設定環境變數,就應該能用了。
      sudo ln -s /mnt/rasp-pi-rootfs/lib/arm-linux-gnueabihf/libdl-2.13.so /mnt/rasp-pi-rootfs/lib/arm-linux-gnueabihf/libdl.so
      sudo ln -s  /mnt/rasp-pi-rootfs/lib/arm-linux-gnueabihf/libm-2.13.so  /mnt/rasp-pi-rootfs/usr/lib/arm-linux-gnueabihf/libm.so
      出現上述問題的原因
      

      in that case the fixQualifiedLibraryPaths won’t help you as it can’t find the symlinks. Copying libdl.so and libm.so might also fail, for example, if you use a flash drive to copy data from your existing Raspberry Pi, it won’t copy them as symlinks, but will copy the libraries themselves. However, for the build to succeed, it seems to require symlinks.

    • 沒有找到檔案問題
      在make 時發生如下問題

      /vc/lib -L/home/liu/bin/opt/qt5/qtlocation/lib -lQt5Location -L/home/liu/bin/opt/qt5/qtbase/lib -L/home/liu/bin/opt/qt5/qtdeclarative/lib -lQt5Quick -lQt5Gui -lQt5Qml -lQt5Network -lQt5Positioning -lQt5Core -lGLESv2 -lpthread -lpoly2tri 
      /home/liu/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.7.2/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lpoly2tri
      collect2: error: ld returned 1 exit status
      Makefile:281: recipe for target '../../../qml/QtLocation/libdeclarative_location.so' failed
      make[4]: *** [../../../qml/QtLocation/libdeclarative_location.so] Error 1
      make[4]: Leaving directory '/home/liu/bin/opt/qt5/qtlocation/src/imports/location'
      Makefile:80: recipe for target 'sub-location-install_subtargets' failed
      make[3]: *** [sub-location-install_subtargets] Error 2
      make[3]: Leaving directory '/home/liu/bin/opt/qt5/qtlocation/src/imports'
      Makefile:135: recipe for target 'sub-imports-install_subtargets' failed
      make[2]: *** [sub-imports-install_subtargets] Error 2
      make[2]: Leaving directory '/home/liu/bin/opt/qt5/qtlocation/src'
      Makefile:56: recipe for target 'sub-src-install_subtargets' failed
      make[1]: *** [sub-src-install_subtargets] Error 2
      make[1]: Leaving directory '/home/liu/bin/opt/qt5/qtlocation'
      Makefile:366: recipe for target 'module-qtlocation-install_subtargets' failed
      make: *** [module-qtlocation-install_subtargets] Error 2

      解決辦法其原因可能是,make配置檔案和原始檔不匹配造成的,請重新獲取原始碼,重新編譯。

    • 啟動不出來qt介面問題
      問題描述:在終端啟動程式後,測試字元都已經顯示,但圖形渲染沒有出現。因為這個為題讓我,真正工作了三天!!

      解決方案
      樹莓派上安裝qt-default其版本是5.3.2,交叉編譯時請編譯對應版本。安裝qtcreator,再啟動測試demo時圖形介面就渲染出來了。

    • 樹莓派上啟動qt程式發現不了裝置等,百度即可

遠端部署測試結果

測試demo

引用連結