0

I am trying to build an application that uses the LibUSB library.

In a previous question I asked here I was told to use find_path and find_library to make CMake search for the headers and binaries. However even after manually looking up the library's installation locations with dnf and specifying them as PATHS or HINTS I always still get the error:

/usr/bin/ld: cannot find -lUSB
collect2: error: ld returned 1 exit status

Below is the relevent cmakelists.txt, my import line in main.cpp is #include <libusb-1.0/libusb.h>

add_executable(project main.cpp)
find_path(LIBUSB_INCLUDE_DIR
  NAMES libusb.h
  PATHS "/usr/include/"
  PATH_SUFFIXES "include" "libusb")
find_library(LIBUSB_LIBRARY
  NAMES USB
  HINTS "/usr/lib/" "/usr/lib64/" "/usr/include/"
  PATH_SUFFIXES "lib" "lib32" "lib64")

target_include_directories(project PRIVATE "/usr/lib/" "/usr/lib64/")
target_link_libraries(project USB)

Clearly I'm doing something wrong in this kludge of hacks, but could someone tell me what?

Community
  • 1
  • 1
Segfault
  • 97
  • 1
  • 3
  • 11

2 Answers2

1

You're not using the result of your find operations anywhere. You tell CMake to find the headers and store the found paths in LIBUSB_INCLUDE_DIR, and to find the library and store its location in LIBUSB_LIBRARY, and then you go to ignore these and use hardcoded "/usr/lib/" "/usr/lib64/" and USB instead. Try this:

target_include_directories(project PRIVATE ${LIBUSB_INCLUDE_DIR})
target_link_libraries(project ${LIBUSB_LIBRARY})
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • I tried your solution, however my error changed to: `/usr/bin/ld: CMakeFiles/botspotptp.dir/main.cpp.o: undefined reference to symbol 'libusb_set_debug' /usr/lib64/libusb-1.0.so.0: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status` What is a DSO? – Segfault Jan 27 '16 at 08:40
  • @LordNibbler Have you asked uncle Google? Perhaps you should review a few tutorials on linking "manually," before CMake enters the picture. – Angew is no longer proud of SO Jan 27 '16 at 09:10
  • I don't see what manually linking has to do with CMake; besides as any good internet citizen I thoroughly scoured google before posting here. I found a reference to needing pthread, which I found and included using the same method, but that did not resolve or change the issue. – Segfault Jan 27 '16 at 10:34
  • @LordNibbler I suggested that because a "DSO" (dynamic shared object, IIUC) has nothing to do with CMake. And because you were talking about include directories and headers in the context of unresolved references. – Angew is no longer proud of SO Jan 27 '16 at 10:48
0

As shown Back in the original question, here, all of the finding and including functions can be replaced simply with:

target_link_libraries(project_name <other_dependencies> usb-1.0),

in the file where the build target is defined.

Community
  • 1
  • 1
Segfault
  • 97
  • 1
  • 3
  • 11