kie*_*bui 5 linux qt udev qt5 libudev
我正在编写一个应用程序自动检测设备是否插入/拔出。
我在 Qt 框架中使用了 C++。libudev.h包含在我的代码中。我实际上通过sudo apt-get install libudev-dev以下方式成功安装了 libudev-dev 包,但 QtCreator 仍然有一条错误消息:libudev development package not found
文件.pro:
...
CONFIG += console c++11
CONFIG -= app_bundle
unix: CONFIG += link_pkgconfig
unix: PKGCONFIG += libudev
HEADERS += DeviceManager.h
SOURCES += main.cpp \
DeviceManager.cpp
...
Run Code Online (Sandbox Code Playgroud)
设备管理器.h文件:
#ifndef DEVICEMANAGER_H
#define DEVICEMANAGER_H
#include <QObject>
#include <QDebug>
#include <QSet>
#include <libudev.h>
#include "DeviceModel.h"
class DeviceManager : public QObject
{
Q_OBJECT
public:
explicit DeviceManager(QObject *parent = 0);
QSet<DeviceModel *> getDevices();
QStringList getDevicePaths();
private:
QSet<DeviceModel *> pluggedDevices;
};
#endif // DEVICEMANAGER_H
Run Code Online (Sandbox Code Playgroud)
DeviceManager.cpp 文件:
#include "DeviceManager.h"
DeviceManager::DeviceManager(QObject *parent) :
QObject(parent)
{
}
QSet<DeviceModel *> DeviceManager::getDevices()
{
QSet<DeviceModel *> devices = QSet<DeviceModel *>();
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *dev_list, *dev_list_entry;
struct udev_device *dev;
struct udev_monitor *mon;
/* Create the udev object */
udev = udev_new();
if (!udev)
{
qDebug() << "Can't create udev";
return devices;
}
/* Set up a monitor to monitor hidraw devices */
mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", "usb_device");
udev_monitor_enable_receiving(mon);
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "usb");
udev_enumerate_scan_devices(enumerate);
dev_list = udev_enumerate_get_list_entry(enumerate);
QString vendorID, serialNumber, devName, deviceName;
udev_list_entry_foreach(dev_list_entry, dev_list)
{
const char *path;
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
if (!dev)
{
qDebug() << "Unable to find parent usb device.";
return devices;
}
vendorID = udev_device_get_property_value(dev, "ID_VENDOR_ID");
devName = udev_device_get_property_value(dev, "DEVNAME");
if (!vendorID.isNull() && vendorID.compare("04e8") == 0) // vendor ID of Samsung devices
{
DeviceModel *device = new DeviceModel();
serialNumber = udev_device_get_property_value(dev, "ID_SERIAL_SHORT");
deviceName = QString(udev_device_get_property_value(dev, "DEVPATH")).split("/").last();
if (!serialNumber.isNull())
{
device->setDownloadMode(false);
device->setSerialNumber(serialNumber);
}
else if(!devName.isNull())
{
device->setPath(devName);
device->setDownloadMode(true);
}
else
break;
device->setName(deviceName);
devices.insert(device);
}
udev_device_unref(dev);
}
/* Free the enumerator object */
udev_enumerate_unref(enumerate);
udev_unref(udev);
this->pluggedDevices = devices;
return devices;
}
QStringList DeviceManager::getDevicePaths()
{
QStringList result;
if (this->pluggedDevices.isEmpty())
{
return result;
}
foreach (DeviceModel *device, this->pluggedDevices)
{
result.append(device->getPath());
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
环境:
操作系统:Ubuntu 18.04.2 LTS
Qt Creator 4.5.2 基于 Qt 5.9.5 (GCC 7.3.0, 64bit)
你能帮我找个理由吗?
小智 11
我用这个,它就像一个魅力
sudo apt-get install libudev-dev
Run Code Online (Sandbox Code Playgroud)
还有一个是这个,我在网上找到的,但没试过。
build/install-build-deps.sh
Run Code Online (Sandbox Code Playgroud)
错误消息表明您丢失了libudev-dev或pkg-config. 既然您已经安装了libudev-dev,那么这意味着您缺少的是该pkg-config工具。如果命令:
pkg-config --version
不打印类似的东西:
0.29.1
那么你就缺少这个pkg-config工具。安装它:
sudo apt install pkg-config
这应该可以解决您的问题。请记住LIBS += -ludev从您的项目文件中删除。你只需要PKGCONFIG += libudev.
| 归档时间: |
|
| 查看次数: |
6611 次 |
| 最近记录: |