我正在编写一个应用程序自动检索设备已插入/插入。
我将C 与QT框架一起使用。 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
...
devicemanager.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
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;
}
环境:
OS:Ubuntu 18.04.2 LTS
QT创建者4.5.2基于QT 5.9.5(GCC 7.3.0,64bit(
您能帮我找到一个原因吗?
错误消息表明您要么缺少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
。
我使用它,它就像魅力
一样起作用sudo apt-get install libudev-dev
另一个是这个,我在互联网上找到了它,但没有尝试过。
build/install-build-deps.sh
您需要将其安装到系统中。您只需尝试这些命令
sudo apt -get更新-y
sudo apt-get安装-y libudev-dev
如果在安装libudev-dev
和pkg-config
后仍然存在问题,请检查另一件事是是否使用了正确的pkg-config
。运行which pkg-config
,对于Ubuntu,应该说/usr/bin/pkg-config
。对我来说,这是错误的/home/linuxbrew/.linuxbrew/bin/pkg-config
,这是由于我的PATH
上的/home/linuxbrew/.linuxbrew/bin/
。然后,我从我的PATH
中删除了Linuxbrew Bin文件夹,并且错误消失了。