libudev development package not found
Asked Answered
E

4

13

I am writing an app autodetect devices is plugged/unplugged.

I used C++ with the Qt framework. libudev.h was included on my code. and I actually installed libudev-dev package successful via sudo apt-get install libudev-dev but QtCreator still has an error message: libudev development package not found

file .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 file:

#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 file:

#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;
}

Environment:

OS: Ubuntu 18.04.2 LTS

Qt Creator 4.5.2 Based on Qt 5.9.5 (GCC 7.3.0, 64bit)

Could you help me find a reason?

Echinus answered 2/5, 2019 at 2:39 Comment(9)
show your .pro... Try add LIBS += -ludevAfrikah
it still isn't work with me.Echinus
show your .pro and provide a minimal reproducible exampleAfrikah
thanks. I updated the content. Could you help me find the problem? I think that it doesn't relate to the source code. It works well on another machine with Ubuntu14.04Echinus
What is the ouput of this command: find /usr/lib/pkgconfig -name "*udev*"Archaic
Also, when replying to comments, tag the people you reply to with @user, otherwise we won't see your replies.Archaic
@nikos-c no noputEchinus
Did you actually install pkg-config? Do sudo apt install pkg-config. Then try again.Archaic
@nikos-c It worked well after install pkg-config successful. ThanksEchinus
A
10

The error message suggests you are either missing libudev-dev or pkg-config. Since you already installed libudev-dev, then that means what you're missing is the pkg-config tool. If the command:

pkg-config --version

doesn't print something like:

0.29.1

then you're missing the pkg-config tool. Install it:

sudo apt install pkg-config

This should fix your issue. Remember to delete LIBS += -ludev from your project file. You only need PKGCONFIG += libudev.

Archaic answered 2/5, 2019 at 7:20 Comment(0)
N
10

I use this and it works like a charm

sudo apt-get install libudev-dev

another one is this, I found it on the internet but haven't tried.

build/install-build-deps.sh
Nutria answered 15/1, 2022 at 11:59 Comment(1)
Did you even read the question? Specifically, the bit that says "I actually installed libudev-dev package successful [sic] via sudo apt-get install libudev-dev"?Fireworks
F
3

You need to install it into your system. You can simply try these commands

sudo apt-get update -y

sudo apt-get install -y libudev-dev

Fleabane answered 22/7, 2022 at 18:1 Comment(1)
Not only does this merely duplicate an earlier answer, but it appears you didn't even read the question. It specifically says says "I actually installed libudev-dev package successful [sic] via sudo apt-get install libudev-dev".Fireworks
B
1

If you still have the problem after installing both libudev-dev and pkg-config, another thing to check is whether the correct pkg-config is being used. Run which pkg-config and for Ubuntu it should say /usr/bin/pkg-config. For me it said /home/linuxbrew/.linuxbrew/bin/pkg-config which is wrong, due to having /home/linuxbrew/.linuxbrew/bin/ on my PATH. I then removed linuxbrew bin folder from my PATH and the error went away.

Battology answered 28/11, 2022 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.