【问题标题】:Compiler error libusb [closed]编译器错误 libusb [关闭]
【发布时间】:2013-02-25 17:40:38
【问题描述】:

我正在运行 Ubuntu 12.04 并使用 c++ 我有以下代码

#include <iostream>
#include <libusb-1.0/libusb.h>

using namespace std;

int main(){
    //pointer to pointer of device used to retrieve a list of devices
    libusb_device **devs;
    libusb_device_handle *dev_handle; //a device handle
    libusb_context *ctx = NULL; //A LIBUSB session
    int r;// for return values
    ssize_t cnt; //holding number of devices in list
    r = libusb_init(&ctx); // initialize the library for the session we just declared

    if(r < 0){
        cout <<"init error "<<r<< endl;
        return 1;
    }

    libusb_set_debug(ctx, 3); // set verbosity level to 3, as suggested in the documentation
    cnt = libusb_get_device_list(ctx, &devs); //get the list of devices

    if (cnt < 0) {
        cout <<"Get Device Error "<< endl; // there was an error
        return 1;
    }

    cout << cnt <<" Device in list " << endl;
    dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689); // these are vendor id and product id

    if (dev_handle == null){
        cout <<"Cannot open device "<< endl;
    }else{
        cout << "Device opened" << endl;
    }

    libusb_free_device_list(devs, 1);// free the list unref the devices in it

    unsigned char *data = new unsigned char[4];//data to write
    data[0] = 'a'; data[1] = 'b'; data[2] = 'c'; data[3] = 'd';//some dummy values

    int actual; //used to find how many bytes were written

    if (libusb_kernal_driver_active(dev_handle, 0) == 1){// findout if kernal driver attached
        cout << "Kernal Driver Active" << endl;
        if (libus_detach_kernal_driver(dev_handle, 0) == 0 ){   //detach it
            cout<< "Kernal Driver Detached" << endl;
        }
    }

    r = libusb_claim_interface(dev_handle, 0);// claim interface 0 (the first) of devices

    if(r < 0){
        cout <<"Cannot claim interface "<<endl;
        return 1;
    }

    cout <<"Claimed interface "<<endl;

    cout<<"data->"<<data<<"<-"<<endl; // just to see the data we want to write : abcd
    cout<<"Writing data..."<<endl;

    r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);//my device's out endpoint was 2, found withe trial - the device had two endpoints: 2 and 129

    if(r == 0 && actual == 4){  // we wrote 4 bytes successfully
        cout<<"Writing successfull"<<endl;
    }else{
        cout<<"write error"<<endl;
    }

    r = libusb_release_interface(dev_handle, 0); // release the claimed interface

    if (r != 0){
        cout << "Cannot release interface" << endl;
        retrun 1;
    }
    cout<<"Released interface"<<endl;
    libusb_close(dev_handle); // close the device we opened
    libusb_exit(ctx); // need to be called to end the

    delete[] data;// delete the allocated memory for data
    return 0;
    //printf("Hello libusb!");
    //return 0;
}

我尝试了以下三个命令来编译上面的代码

g++ transfer_data_libusb.cpp $(pkg-config --libs libusb-1.0) -o transfer_data_libusb
g++ transfer_data_libusb.cpp -o transfer_data_libusb
g++ transfer_data_libusb.cpp

但我收到以下错误

transfer_data_libusb.cpp: In function ‘int main()’:
transfer_data_libusb.cpp:32:20: error: ‘null’ was not declared in this scope
transfer_data_libusb.cpp:45:47: error: ‘libusb_kernal_driver_active’ was not declared in this scope
transfer_data_libusb.cpp:47:47: error: ‘libus_detach_kernal_driver’ was not declared in this scope

我很难理解这些错误,如果可能的话,我希望得到一些解决方案的建议,

谢谢

【问题讨论】:

    标签: c++ ubuntu libusb


    【解决方案1】:

    您收到错误是因为此变量和函数(符号)未在您的主程序或您使用的库(头文件)中声明。您可能只是拼错了他们的名字。

    • 您应该使用“NULL”而不是“null”
    • 您应该使用“libusb_kernel_driver_active”而不是“libusb_kernal_driver_active”
    • 您应该使用“libus_detach_kernel_driver”而不是“libus_detach_kernal_driver”

    只要解决这个问题,一切都会好起来的。

    【讨论】:

    • 非常感谢,我已经按照您的建议进行了更改,现在程序可以完美编译了。当实际供应商 ID 和设备 ID 替换代码中的虚拟版本时,我似乎遇到了新问题。它不断给出另一个错误,即:(transfer_data_libusb.cpp:30:55: error: invalid digit "9" in octal constant)你能帮我解决这个问题吗,顺便说一句,我使用的 USB 设备是 16gb 金士顿设备
    • 你可能有一些问题:dev_handle = libusb_open_device_with_vid_pid(ctx, 0951, 1689);
    • @UsmanSharifAmjadKhan 当您将 0 放在数字常量前面时,它将被解释为八进制。 9 不是八进制数字。
    • 当您尝试将一个合法数字写入八进制常量时会出现此错误。如果它是八进制常数,那么唯一有效的数字是 0 到 7,任何其他数字都不能放入。当您尝试写“08”而不是“8”时,这会使编译器将数字写入以 8 为基数的变量中,这是非法的。
    • 你能给我一个示例更改以下内容吗:0951、1689 这些是我的 USB 的供应商 ID 和产品 ID
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多