How to solve linking issues with libusb in make file

288 Views Asked by At

I have been wrestling with cmake for hours upon end now but I am getting it to build make files now but I am struggling to get the make command to work properly. I believe it is and issue with my libusb links but I am pretty clueless. I am using the cmake GUI on windows. I will provide screenshots of what my configurations are enter image description here I believe that these are all how they should be apart from the LIBUSB_1_LIBRARY I think as its not an actual .dll file (its a .dll.a) but I cannot find a libusb-1.0.dll anywhere. In my libusb-MinGW/lib folder I have libusb-1.0.a, libusb-1.0.dll.a and libusb-1.0.la also i have a pkgconfig file containing libusb-1.0.pc so I am not sure if the correct one is there or not. There is also a dll in libusb-MinGW/bin folder called msys-usb-1.0.dll that being the only .dll file i could find so maybe it is that one I am not sure. I apologise in advance if this is a very trivial question but I am very new to anything C related I only have experience with python and java really so this is very much uncharted territory for me and there doesn't seem to be much documentation on how to install libfreekinect apart from the github page but I have looked at that for ages and I am getting nothing from it.

  • EDIT - I completely forgot to include the output from when i run the make command. I get a big wall of text that give the same thing with varying output i will provide a few lines
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\freenect.dir/objects.a(tilt.c.obj):tilt.c:(.text+0x4b): undefined reference to `libusb_bulk_transfer'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\freenect.dir/objects.a(tilt.c.obj):tilt.c:(.text+0xbf): undefined reference to `libusb_error_name'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\freenect.dir/objects.a(tilt.c.obj):tilt.c:(.text+0x1cc): undefined reference to `libusb_bulk_transfer'
1

There are 1 best solutions below

0
Tim Seed On

I am having the same issue (Using MSYS64)

I have built from source libusb(1.0) like this

cd libusb
./configure --prefix /usr/local
make -j4
make install  

I can see the files in /usr/local/lib

And there is a .pc file in */usr/local/lib/pkgconfig/libusb-1.0.pc

So - I go and create a Project that needlessly links to this library

cd Dev
mkdir TestCm && cd TestCm

Inside this I place a "Hello.world" C file.. and nothing else.

Then I create a CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(TT)
set(SOURCE hello.c)
add_executable(T ${SOURCE})
include(FindPkgConfig)
pkg_checK_modules(USB libusb-1.0)
if (USB_FOUND)
        message(STATUS "libusb-1.0 was found")
        target_link_libraries(T ${USB_LIBRARIES})
else()
        message(WARNING "libusb-1.0 was not found")
endif()

So build it ...

mkdir build && cd build 
cmake ../  

I see the Info message libusb-1.0 was found.

Now to compile

make VERBOSE=1

And I get an error with

-lusb-1.0: No such library or directory 

I manually check the /usr/local/lib/pkgconfig/linusb-1.0.pc

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: libusb-1.0
Description: C API for USB device access from Linux, Mac OS X, Windows, OpenBSD/NetBSD and Solaris userspace
Version: 1.0.26
Libs: -L${libdir} -lusb-1.0
Libs.private:  -Wl,
Cflags: -I${includedir}/libusb-1.0

This looks ok. -L{$libdir} is however missing from the Link ...

Enter ccmake

I double checked this, using ccmake ..

Switched to advanced mode (press t), and scrolled to the bottom where I found pkg_config_lib_USB_libusb-1.0 and it was pointing to a different directory (/mingw64/lib/linusb-1.0.a)

The CMake is now altered to be

cmake_minimum_required(VERSION 3.15)
project(TT)
set(SOURCE hello.c)
add_executable(T ${SOURCE})

include(FindPkgConfig)
pkg_checK_modules(USB libusb-1.0)
if (USB_FOUND)
        message(STATUS "libusb-1.0 was found")
        #       target_link_libraries(T ${USB_LIBRARIES})
        target_link_libraries(T ${pkg_config_lib_USB_libusb-1.0})
else()
        message(WARNING "libusb-1.0 was not found")
endif()

Build (again)

so

cmake ../
make VERBOSE=1

It now build correctly.

I hope this may assist you in trying to find the issue.