Search code examples
cmakecross-compiling

How to set LIBUSB_INCLUDE_DIR


I'm trying to cross-compile libftdi for arm. When I run Cmake I get:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIBUSB_INCLUDE_DIR (ADVANCED)

I understand that LIBUSB_INCLUDE_DIR ought to be set in FindUSB1.cmake found in the libftdi root directory. However, I have no clue on how to make FindUSB1.cmake find libusb that I have compiled and put into /opt/lib. This is the default file:

# - Try to find the freetype library
# Once done this defines
#
#  LIBUSB_FOUND - system has libusb
#  LIBUSB_INCLUDE_DIR - the libusb include directory
#  LIBUSB_LIBRARIES - Link these to use libusb

# Copyright (c) 2006, 2008  Laurent Montel, <[email protected]>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.


if (LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)

  # in cache already
  set(LIBUSB_FOUND TRUE)

else (LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
  IF (NOT WIN32)
    # use pkg-config to get the directories and then use these values
    # in the FIND_PATH() and FIND_LIBRARY() calls

    find_package(PkgConfig)
    pkg_check_modules(PC_LIBUSB libusb-1.0)
  ENDIF(NOT WIN32)

  FIND_PATH(LIBUSB_INCLUDE_DIR libusb.h
    PATHS ${PC_LIBUSB_INCLUDEDIR} ${PC_LIBUSB_INCLUDE_DIRS})

  FIND_LIBRARY(LIBUSB_LIBRARIES NAMES usb-1.0
    PATHS ${PC_LIBUSB_LIBDIR} ${PC_LIBUSB_LIBRARY_DIRS})

  include(FindPackageHandleStandardArgs)
  FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBUSB DEFAULT_MSG LIBUSB_LIBRARIES LIBUSB_INCLUDE_DIR)

  MARK_AS_ADVANCED(LIBUSB_INCLUDE_DIR LIBUSB_LIBRARIES)

endif (LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)

In /opt/lib there is a directory pkconfig. In this directory there is a libusb-1.0.pc file that I guess might have something to do with the problem. So how should I get Cmake to find /opt/lib/libsub-1.0?


Solution

  • Unfortunately, the default find script is not very powerful. If the library is not located in one of the default directories searched by find_library, you have to rely on pkg-config to tell you where the library is located.

    In the (not so unlikely) event that the library is not registered with pkg-config either, you have no choice but setting the PC_LIBUSB_LIBDIR and PC_LIBUSB_INCLUDEDIR variables manually before calling the find script. This can be done either from the command line (using the -D parameter) or from within the enclosing CMake script (using set).

    If you are at liberty to make changes to the find script, consider adding additional HINTS to the find calls to make them more flexible. For instance, using an environment variable as a hint is a quite popular mechanism that also works well for non-Unix platforms.

    FIND_PATH(LIBUSB_INCLUDE_DIR libusb.h
        HINTS $ENV{LIBUSB_ROOT}
        PATHS ${PC_LIBUSB_INCLUDEDIR} ${PC_LIBUSB_INCLUDE_DIRS}
        PATH_SUFFIXES include)
    
    FIND_LIBRARY(LIBUSB_LIBRARIES NAMES usb-1.0
        HINTS $ENV{LIBUSB_ROOT}
        PATHS ${PC_LIBUSB_LIBDIR} ${PC_LIBUSB_LIBRARY_DIRS}
        PATH_SUFFIXES lib)
    

    Since CMake version 3.12, CMake will automatically take the <PackageName>_ROOT variables into account as hints for calls to find_package calls, and all nested find_* calls made from within a find script that was triggered by a call to find_package. So in those cases, you won't have to specify them manually as hints anymore. Note that for this to work, your cmake_minimum_required version needs to be set to 3.12 or higher.