1

In my CMake project, I'm copying external libraries that I hold in a special folder to my output folder and import them as an IMPORTED target.

For example, for libusb:

find_path(LibUsb_INCLUDE_DIR NAMES libusb.h PATHS ${CMAKE_CURRENT_SOURCE_DIR}/LibUsb/Include)
if(NOT LibUsb_INCLUDE_DIR)
message(FATAL_ERROR "LibUsb: include directory wasn't found")
endif()

find_library(LibUsb_LIBRARY NAMES usb-1.0 libusb-1.0 PATHS ${OUTPUT_BIN_DIR})
if(NOT LibUsb_LIBRARY)
    message(FATAL_ERROR "LibUsb: library wasn't found")
endif()

add_library(LibUsb UNKNOWN IMPORTED GLOBAL)
set_target_properties(LibUsb PROPERTIES IMPORTED_LOCATION ${LibUsb_LIBRARY})
set_target_properties(LibUsb PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LibUsb_INCLUDE_DIR})

message(STATUS "LibUsb: found at ${LibUsb_LIBRARY}")

The CMake output is:

...
-- Building for: Visual Studio 15 2017
-- The C compiler identification is MSVC 19.0.24234.1
-- The CXX compiler identification is MSVC 19.0.24234.1
...
-- CMAKE SYSTEM: Windows-10.0.17763
-- CMAKE VERSION: 3.13.4
...
-- LibUsb: copy binaries
-- VsCan: copy binaries
-- LibUsb: found at C:/myproj/Build/Windows-Debug/Output/Bin/libusb-1.0.lib
-- VsCan: found at C:/myproj/Build/Windows-Debug/Output/Bin/vs_can_api.lib
...

LibUsb is found correctly and if I print LibUsb_LIBRARY the path is correct. Everything compiles correctly on Linux.

On Windows compilation I'm getting fatal error LNK1107: invalid or corrupt file.

What am I doing wrong?

galah92
  • 3,621
  • 2
  • 29
  • 55
  • Value `LibUsb-NOTFOUND` is for variable named `LibUsb`, not the `LibUsb_LIBRARY_DIR` or `LibUsb_INCLUDE_DIR` which you show us. – Tsyvarev Apr 15 '19 at 16:22
  • @Tsyvarev but `LibUSB` is a target, not a variable – galah92 Apr 15 '19 at 16:24
  • Hmm, that means that some target's property is *missed*... BTW, `.obj` extension looks unusual for "NOTFOUND" things. – Tsyvarev Apr 15 '19 at 17:27
  • Googling for `NOTFOUND.obj` reveals that question: https://stackoverflow.com/questions/14850736/cmake-imported-library-behaviour, which seems to describe your problem. `find_library` searches for `.lib` file even for `.dll`, but in case of `SHARED IMPORTED` library its `IMPORTED_LOCATION` property should point to `.dll` itself. Try to change `SHARED IMPORTED` to `UNKNOWN IMPORTED`. – Tsyvarev Apr 15 '19 at 19:55
  • @Tsyvarev Thanks, but it's not working. I'm getting `fatal error LNK1107`. I guess my underlying question is how to create imported library in a cross platform manner, because `IMPORTED_LOCATION` should be `lib` for linux but `dll` for windows. – galah92 Apr 16 '19 at 05:51
  • Please, show output of `LibUsb_INCLUDE_DIR` and `LibUsb_LIBRARY_DIR` variables which you got with `message()` call. (Note: the variable's name `LibUsb_LIBRARY_DIR` is misleading, because it points not to a *directory*, as `DIR` suffix assume, but to a **file**). Also, point in the question post which version of Visual Studio do you use. – Tsyvarev Apr 16 '19 at 06:00
  • Hm, is `libusb-1.0.lib` an *import* file for shared library or is it a *static* library? – Tsyvarev Apr 16 '19 at 11:35
  • imported. I've got `libusb-1.0.lib` and `libusb-1.0.dll` – galah92 Apr 16 '19 at 12:25

0 Answers0