gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:45:38 24 4
gpt4 key购买 nike

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

在 Qt Creator 中创建名为 SlaveDeviceModels 的静态库。我将这个库部署到树莓派上并在 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 文件。问候。

关于c++ - Qt项目自定义共享库交叉编译Qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49338049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com