Qt项目自定义共享库交叉编译Qt

Qt project with custom shared library cross compile Qt

我在 Raspberry Pi 上构建了用于交叉编译的 Qt 5.9.4 3. 测试应用程序一切正常。我的问题如下:

在 Qt Creator 中创建名为 SlaveDeviceModels 的静态库。我将这个库部署到 raspberry 并在 rpi 上 运行 这个命令:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/pi/Documents/QtProjectsTests/SmartHomeServer

然后我创建了名为 SmartHomeServer 的控制台应用程序项目并添加到该项目外部库 SlaveDeviceModels。然后我尝试通过 Qt Creator 运行 这个应用程序,但我得到错误:

/home/pi/Documents/QtProjectsTests/SmartHomeServer/SmartHomeServer: error while loading shared libraries: libSlaveDeviceModels.so.1: cannot open shared object file: No such file or directory
Application finished with exit code 127.

所以我转到 rpi 文件夹,它看起来如下:

pi@raspberrypi:~/Documents/QtProjectsTests/SmartHomeServer $ ls -l
total 3048
-rwxr-xr-x 1 pi pi 2122400 Mar 17 14:10 SmartHomeServer
-rw-r--r-- 1 pi pi    9006 Mar 17 14:04 SmartHomeServer.log
lrwxrwxrwx 1 pi pi      29 Mar 17 13:28 libSlaveDeviceModels.so -> libSlaveDeviceModels.so.1.0.0
lrwxrwxrwx 1 pi pi      29 Mar 17 13:28 libSlaveDeviceModels.so.1 -> libSlaveDeviceModels.so.1.0.0
lrwxrwxrwx 1 pi pi      29 Mar 17 13:28 libSlaveDeviceModels.so.1.0 -> libSlaveDeviceModels.so.1.0.0
-rwxr-xr-x 1 pi pi  980428 Mar 17 13:28 libSlaveDeviceModels.so.1.0.0

然后我尝试 运行 这个应用 ./SmartHomeServer 并且它工作正常。开始了。

唯一的问题是当我试图从本地计算机上的 Qt Creator 启动 SmartHomeServer(交叉编译)时。有没有人可以帮助我哪里可能有问题。也许缺少 Qt Creator 中的设置?

这是我的 SmartHomeServer.pro 文件:

QT += core
QT -= gui

CONFIG += c++11

TARGET = SmartHomeServer
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

# Default rules for deployment.
target.path = /home/pi/Documents/QtProjectsTests/SmartHomeServer
INSTALLS += target

# here are included my headers
INCLUDEPATH += $$PWD/../SlaveDeviceModels

# I tried do something like that
#LIBS += -L/home/pi/Documents/QtProjectsTests/SmartHomeServe -lSlaveDeviceModels

# that are default lines added by qt creator
unix:!macx: LIBS += -L$$PWD/../build-SlaveDeviceModels-Raspberry_Pi_3_Kit-Debug/ -lSlaveDeviceModels
INCLUDEPATH += $$PWD/../build-SlaveDeviceModels-Raspberry_Pi_3_Kit-Debug
DEPENDPATH += $$PWD/../build-SlaveDeviceModels-Raspberry_Pi_3_Kit-Debug

一种解决方法是将这些代码行添加到您的 .pro 文件中:

unix:!mac {
    LIBS += -Wl,-rpath=\$$ORIGIN/libs
}

所以现在您可以将所有需要的库放在可执行文件的 libs 子文件夹中。

Windows 或 Mac 不需要此方法。 请记住为 Qt 插件使用 qt.conf 文件。 问候。