I am new to the whole linux and c/c++ development world and I'm trying to port one of my windows programs to Linux. I am stuck at figuring out how to get USB Devices like memory sticks, external hard drives that are connected to the system as well as their addresses so that they can be used with certain commandline utilities.
My development environment:
- OS: Zorin OS 15 64-bit
- IDE: Code::Blocks 16
- Compiler: GCC
- GUI: WxWidgets 3
- Development Language: C++
I know on windows you can use GetLogicalDrives() to get the drives that are attached to the system and in such a way determine if the device is USB or not. I have tried to use libusb (with it's header file), but cannot figure out how to get the devices and add them to a dropdown box.
I have used the following code from the libusb docs:
static void print_devs(libusb_device **devs)
{
libusb_device *dev;
int i = 0, j = 0;
uint8_t path[8];
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
fprintf(stderr, "failed to get device descriptor");
return;
}
printf("%04x:%04x (bus %d, device %d)",
desc.idVendor, desc.idProduct,
libusb_get_bus_number(dev), libusb_get_device_address(dev));
r = libusb_get_port_numbers(dev, path, sizeof(path));
if (r > 0) {
printf(" path: %d", path[0]);
for (j = 1; j < r; j++)
printf(".%d", path[j]);
}
printf("\n");
}
}
but this give me the following error: undefined reference to 'libusb_get_device_descriptor'
.
I have included the libusb.h
file as well as libusb.a
and libusb.so
.
All I want to do is get a list of attached removable usb drives with their addresses (in the form of /dev/sda or similar) so that I can add them to the dropdown box for later use.
When all is said and done, the user will select a USB drive from the drop down, then the app will format the device if the user chooses to do so and then copy some files onto it.
EDIT:
Here is the output of the build log:
-------------- Build: Debug in BlackBoxISOBeta (compiler: GNU GCC Compiler)---------------
g++ -o bin/Debug/BlackBoxISOBeta obj/Debug/BlackBoxISOBetaApp.o obj/Debug/BlackBoxISOBetaMain.o obj/Debug/BurnUSB.o -L/usr/lib/x86_64-linux-gnu -pthread -lwx_gtk2u_xrc-3.0 -lwx_gtk2u_html-3.0 -lwx_gtk2u_qa-3.0 -lwx_gtk2u_adv-3.0 -lwx_gtk2u_core-3.0 -lwx_baseu_xml-3.0 -lwx_baseu_net-3.0 -lwx_baseu-3.0 /usr/lib/x86_64-linux-gnu/libusb.a
obj/Debug/BurnUSB.o: In function `print_devs(libusb_device**)':
/home/mafiafly/Documents/bbiso/BlackBoxISOBeta/BurnUSB.cpp:56: undefined reference to `libusb_get_device_descriptor'
/home/mafiafly/Documents/bbiso/BlackBoxISOBeta/BurnUSB.cpp:64: undefined reference to `libusb_get_device_address'
/home/mafiafly/Documents/bbiso/BlackBoxISOBeta/BurnUSB.cpp:64: undefined reference to `libusb_get_bus_number'
/home/mafiafly/Documents/bbiso/BlackBoxISOBeta/BurnUSB.cpp:66: undefined reference to `libusb_get_port_numbers'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
5 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I get the same error with libusb.a
and libusb.so