1. 程式人生 > >qtcreator下cmake工程交叉編譯及遠端部署環境搭建

qtcreator下cmake工程交叉編譯及遠端部署環境搭建

1 安裝目標開發板對應的交叉編譯器。

首先,在開發板上使用下面命令檢視cpu硬體架構:

uname -a
我的顯示有aarch64字樣,即我的開發板為armv8a-aarch64架構。接下來在開發電腦上使用下面命令來安裝c和c++編譯器:
sudo apt-get install gcc-aarch64-linux-gnu
sudo apt-get install g++-aarch64-linux-gnu

如果你的開發板是arm架構,就使用:

sudo apt-get install gcc-arm-linux-gnueabi
sudo apt-get install g++-arm-linux-gnueabi

安裝後,新建一個main.c檔案

#include<stdio.h>
int main()
{
	printf("hello world!\n");
}
使用命令:
aarch64-linux-gnu-gcc hello.c -o h

可以在當前目錄得到h的可執行檔案,使用命令
file ./h
可以看到輸出:
./h: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=a7ff48d62094e807ba473044eb1792fbf80784da, not stripped

可以看出他是一個ARM aarch64下的可執行檔案,到此,證明交叉編譯器安裝成功了!那麼,這些編譯器安裝在哪裡呢?如下圖/usr中,編譯器程式都放在/usr/bin目錄中。


2 QtCreator開發環境設定

開啟QtCreator,工具-》選項,切換到“構建和執行”,”編譯器“,新增一個gcc編譯器,注意是要選擇對應平臺的g++而不是gcc,gcc是編譯c程式的,g++是編譯c++程式的。

選擇裝置,新建一個linux通用裝置:


選擇構建套件,新增一個構建套件,裝置和編譯器就選擇前兩步建好的專案


好了,到這裡就可以新建一個cmake專案來進行測試了,qtcreator,新建專案,選擇Non-Qt Project,選擇構建工具的時候,選擇cmake,構建套件選擇剛剛建立好的套件。



除錯

安裝多架構支援的偵錯程式:

sudo apt-get install gdb-multiarch

照上面在 偵錯程式裡面新建一個偵錯程式,位置選擇/usr/bin/×××,剛剛安裝到就是這個位置。

在kit裡面選擇剛剛建好的偵錯程式。

問題

1 在遠端執行的時候,發現上傳失敗,原因是上傳路徑設定的不太對,預設是當前目錄.  ,也沒法改,搜尋了一下發現如下方法:

Deploying CMake Projects to Embedded Linux Devices

Qt Creator cannot extract files to be installed from a CMake project, and therefore, only executable targets are automatically added to deployment files. You must specify all other files in theQtCreatorDeployment.txt file that you create and place in either the root directory of the CMake project or the build directory of the active build configuration. Currently, Qt Creator first checks the root directory and only if noQtCreatorDeployment.txt exists it checks the active build directory.

Use the following syntax in the file:

<deployment/prefix><relative/source/file1>:<relative/destination/dir1>...<relative/source/filen>:<relative/destination/dirn>

Where:

  • <deployment/prefix> is the (absolute) path prefix to where files are copied on the remote machine.
  • <relative/source/file> is the file path relative to the CMake project root. No directories or wildcards are allowed in this value.
  • <relative/destination/dir> is the destination directory path relative todeployment/prefix.

To automate the creation of QtCreatorDeployment.txt file:

  1. Define the following macros in the top level CMakeLists.txt file:
    file(WRITE "${CMAKE_SOURCE_DIR}/QtCreatorDeployment.txt""<deployment/prefix>\n")
    
    macro(add_deployment_file SRC DEST)
        file(RELATIVE_PATH path ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
        file(APPEND "${CMAKE_SOURCE_DIR}/QtCreatorDeployment.txt""${path}/${SRC}:${DEST}\n")
    endmacro()
    
    macro(add_deployment_directory SRC DEST)
        file(GLOB_RECURSE files RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}""${SRC}/*")foreach(filename ${files})
            get_filename_component(path ${filename} PATH)
            add_deployment_file("${filename}""${DEST}/${path}")
        endforeach(filename)
    endmacro()
  2. Use add_deployment_file(<file/name>) to add files and add_deployment_directory(<folder/name>) to add directories (including subdirectories) to theQtCreatorDeployment.txt file.
  3. Re-run cmake after you add or remove files using the macros.

因此,只要將改段命令加在你的cmake檔案裡面,將上面的"<deployment/prefix>"改成你想要設定的開發板部署路徑,下面如果要新增其他檔案或資料夾就用巨集:

具體連結:http://doc.qt.io/qtcreator/creator-deployment-embedded-linux.html

add_deployment_file(f1 f2)
add_deployment_directory(fold1 fold2)