带有自定义共享库的 Qt 项目交叉编译 Qt
Posted
技术标签:
【中文标题】带有自定义共享库的 Qt 项目交叉编译 Qt【英文标题】:Qt project with custom shared library cross compile Qt 【发布时间】:2018-03-17 14:34:00 【问题描述】:我在 Raspberry Pi 3 上构建了用于交叉编译的 Qt 5.9.4。在测试应用程序上一切正常。我的问题如下:
在 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
【问题讨论】:
【参考方案1】:一种解决方法是将这些代码行添加到您的 .pro 文件中:
unix:!mac
LIBS += -Wl,-rpath=\\\$$ORIGIN/libs
所以现在您可以将所有需要的库放在可执行文件的 libs 子文件夹中。
Windows 或 Mac 不需要这种方法。 请记住为 Qt 插件使用 qt.conf 文件。 问候。
【讨论】:
以上是关于带有自定义共享库的 Qt 项目交叉编译 Qt的主要内容,如果未能解决你的问题,请参考以下文章
当静态库不可用时,是不是可以从 linux 交叉编译 Windows?
使用动态链接在 Linux 上为 Windows 交叉编译 Qt 应用程序