Search code examples
c++cmakelibusb-1.0

CMake can't find external library


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?


Solution

  • 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.