Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HELP] LNK1104 with msvc compiler #26

Open
AlexanderTonn opened this issue Apr 10, 2024 · 2 comments
Open

[HELP] LNK1104 with msvc compiler #26

AlexanderTonn opened this issue Apr 10, 2024 · 2 comments
Labels
question Further information is requested

Comments

@AlexanderTonn
Copy link

AlexanderTonn commented Apr 10, 2024

Hey fellows,

  • i have currently a problem with an Linker Error LNK1104 of libusb on Windows with the msvc compiler.

This is the configuration with which I'm able to run on debian and macOs (gcc)

CMakeLists.txt
cmake_minimum_required(VERSION 3.22)

project(
    "OWON-HDS2-Viewer"
    VERSION 0.2.0
    LANGUAGES C CXX)

message("PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")    #show the project source directory
message("CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")        #show the cmake source directory

set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/external/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(APPLE)
    set(CMAKE_OSX_ARCHITECTURES "arm64" ) # for apple silicon
elseif(WIN32 OR UNIX)
    set(CMAKE_OSX_ARCHITECTURES "x86_64" ) # for intel
endif()

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/)

option(ENABLE_WARNINGS "Enable to add warnings to a target." ON)
option(ENABLE_WARNINGS_AS_ERRORS "Enable to treat warnings as errors." OFF)
option(ENABLE_CLANG_TIDY "Enable to add clang tidy." ON)
option(ENABLE_CLANG_FORMAT "Enable to add clang-format." ON)

include(Warnings)
#  include(Tools) # needs clang tidy and clang format
include(ConfigSafeGuards)

# add_clang_format_target() # needs python

set (VCPKG_DIR ${CMAKE_SOURCE_DIR}/external/vcpkg)
# you can set any other vcpkg installation on your system
# set (VCPKG_DIR ${CMAKE_SOURCE_DIR}/../../../../../vcpkg)
include(${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake)

find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h)
find_library(LIBUSB_LIBRARY NAMES libusb)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(libusb DEFAULT_MSG LIBUSB_LIBRARY LIBUSB_INCLUDE_DIR)

if(LIBUSB_FOUND AND NOT TARGET libusb::libusb)
  add_library(libusb::libusb UNKNOWN IMPORTED)
  set_target_properties(libusb::libusb PROPERTIES
    IMPORTED_LOCATION "${LIBUSB_LIBRARY}"
    INTERFACE_INCLUDE_DIRECTORIES "${LIBUSB_INCLUDE_DIR}")
endif()

find_package(fmt CONFIG REQUIRED)
find_package(glfw3 CONFIG REQUIRED)
find_package(imgui CONFIG REQUIRED)
find_package(implot CONFIG REQUIRED)
find_package(OpenGL REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBUSB REQUIRED IMPORTED_TARGET libusb-1.0)


add_executable(${PROJECT_NAME}
    src/main.cpp
    src/render.cpp
    src/render.hpp
    src/csvHandler.cpp
    src/csvHandler.hpp
    src/fileHandler.hpp
    src/fileHandler.cpp
    src/usbHid.hpp
    src/usbHid.cpp
    src/usbMsc.hpp
    src/usbMsc.cpp
    src/funcTrigger.hpp
    src/funcTrigger.cpp
    src/guiStrings.hpp
    src/guiStrings.cpp
    src/dialogs.hpp
    src/dialogs.cpp
)

add_compile_definitions(NOMINMAX)

target_set_warnings(TARGET ${PROJECT_NAME}
    ENABLE ${ENABLE_WARNINGS}
    AS_ERRORS ${ENABLE_WARNINGS_AS_ERRORS})
# add_clang_tidy_msvc_to_target(${PROJECT_NAME})

target_include_directories(${PROJECT_NAME} PUBLIC src/)



target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt-header-only)
target_link_libraries(${PROJECT_NAME} PRIVATE glfw)
target_link_libraries(${PROJECT_NAME} PRIVATE OpenGL::GL)
target_link_libraries(${PROJECT_NAME} PRIVATE imgui::imgui)
target_link_libraries(${PROJECT_NAME} PRIVATE implot::implot)
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::LIBUSB)
vcpkg.json
{
    "name": "gui",
    "version-string": "0.2.0",
    "dependencies": [
        {
            "name": "fmt"
        },
        {
            "name": "opengl"
        },
        {
            "name": "glfw3"
        },
        {
            "name": "imgui",
            "default-features": false,
            "features": [
                "glfw-binding",
                "opengl3-binding"
            ]
        },
        {
            "name": "implot"
        },
        {
            "name" : "libusb"
        }
    ]
}

This is what i tried

  • i added debug message for figuring out whether the libusb has been considered by Cmake. And it is
  • i also can see the libusb in the build directory

image

[build] LINK : fatal error LNK1104: Datei "libusb-1.0.lib" kann nicht geöffnet werden.

Following my Modified CMakeLists.txt for getting some more debug informations and which is triggering the Linking error

CMakeLists.txt
cmake_minimum_required(VERSION 3.22)

project(
    "OWON-HDS2-Viewer"
    VERSION 0.2.0
    LANGUAGES C CXX)

message("PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")    #show the project source directory
message("CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")        #show the cmake source directory

set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/external/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(APPLE)
    set(CMAKE_OSX_ARCHITECTURES "arm64" ) # for apple silicon
elseif(WIN32 OR UNIX)
    set(CMAKE_OSX_ARCHITECTURES "x86_64" ) # for intel

endif()

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/)

option(ENABLE_WARNINGS "Enable to add warnings to a target." ON)
option(ENABLE_WARNINGS_AS_ERRORS "Enable to treat warnings as errors." OFF)
option(ENABLE_CLANG_TIDY "Enable to add clang tidy." ON)
option(ENABLE_CLANG_FORMAT "Enable to add clang-format." ON)
option(ENABLE_CPPCHECK "Enable to add cppcheck." ON)

include(Warnings)
#  include(Tools) # needs clang tidy and clang format
include(ConfigSafeGuards)

# add_clang_format_target() # needs python

set (VCPKG_DIR ${CMAKE_SOURCE_DIR}/external/vcpkg)
# you can set any other vcpkg installation on your system
# set (VCPKG_DIR ${CMAKE_SOURCE_DIR}/../../../../../vcpkg)
include(${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake)

find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h)
find_library(LIBUSB_LIBRARY NAMES libusb)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(libusb DEFAULT_MSG LIBUSB_LIBRARY LIBUSB_INCLUDE_DIR)

if(LIBUSB_FOUND AND NOT TARGET libusb::libusb)
  add_library(libusb::libusb UNKNOWN IMPORTED)
  set_target_properties(libusb::libusb PROPERTIES
    IMPORTED_LOCATION "${LIBUSB_LIBRARY}"
    INTERFACE_INCLUDE_DIRECTORIES "${LIBUSB_INCLUDE_DIR}")
endif()

find_package(fmt CONFIG REQUIRED)
find_package(glfw3 CONFIG REQUIRED)
find_package(imgui CONFIG REQUIRED)
find_package(implot CONFIG REQUIRED)
find_package(OpenGL REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBUSB REQUIRED IMPORTED_TARGET libusb-1.0)

message("LIBUSB_INCLUDE_DIRS: ${LIBUSB_INCLUDE_DIRS}")
message("LIBUSB_LIBRARIES: ${LIBUSB_LIBRARIES}")

add_executable(${PROJECT_NAME}
    src/main.cpp
    src/render.cpp
    src/render.hpp
    src/csvHandler.cpp
    src/csvHandler.hpp
    src/fileHandler.hpp
    src/fileHandler.cpp
    src/usbHid.hpp
    src/usbHid.cpp
    src/usbMsc.hpp
    src/usbMsc.cpp
    src/funcTrigger.hpp
    src/funcTrigger.cpp
    src/guiStrings.hpp
    src/guiStrings.cpp
    src/dialogs.hpp
    src/dialogs.cpp
)

add_compile_definitions(NOMINMAX)

target_set_warnings(TARGET ${PROJECT_NAME}
    ENABLE ${ENABLE_WARNINGS}
    AS_ERRORS ${ENABLE_WARNINGS_AS_ERRORS})
# add_clang_tidy_msvc_to_target(${PROJECT_NAME})

target_include_directories(${PROJECT_NAME} PUBLIC src/)
link_directories("C:/Users/A.Tonn/OneDrive - JUMAG Dampferzeuger/Dokumente/OWON-HDS2-Viewer/build/vcpkg_installed/x64-windows/lib/libusb-1.0.lib")


# LINKING:
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt-header-only)
target_link_libraries(${PROJECT_NAME} PRIVATE glfw)
target_link_libraries(${PROJECT_NAME} PRIVATE OpenGL::GL)
target_link_libraries(${PROJECT_NAME} PRIVATE imgui::imgui)
target_link_libraries(${PROJECT_NAME} PRIVATE implot::implot)
# target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::libusb-1.0)
target_link_libraries(${PROJECT_NAME} PRIVATE libusb-1.0)


@tormodvolden tormodvolden transferred this issue from libusb/libusb Apr 10, 2024
@mcuee mcuee added the question Further information is requested label Apr 10, 2024
@AlexanderTonn
Copy link
Author

AlexanderTonn commented Apr 10, 2024

I found a solution for me but it doesnt look very "clean" to me and i have to test whether it's still working with macOs and Linux

I changed my CMakeLists.txt to:

set(LIBUSB_LIBRARY_PATH ${CMAKE_SOURCE_DIR}/build/vcpkg_installed/x64-windows/lib/libusb-1.0.lib)

# .... 

target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBUSB_LIBRARY_PATH})

@Youw
Copy link
Member

Youw commented Apr 10, 2024

For starters - you've originally reported an issue in the original libusb repo https://github.com/libusb/libusb/issues/1480 .
libusb team officially does not claim to support CMake as a build system (at least withing https://github.com/libusb/libusb repo).

Thanks to @tormodvolden the issue is now moved to libusb-cmake repo which only purpose is to have a CMake build system/scripts for the original libusb project.

What you're trying to do - come up with your own script to use libusb in some way. Which, apparently, is not ideal.

If your goal is "just use libusb" - I recommend using libusb-cmake directly.
For you it should be as simple as:

add_subdirectory(libusb-cmake)

target_link_libraries(${PROJECT_NAME} PRIVATE libusb::libusb)

This method assumes you'll add libusb-cmake as a submodule or so.

Similar alternative is to use FetchContent:

FetchContent_Declare(
  LibusbCmake
  GIT_REPOSITORY https://github.com/libusb/libusb-cmake.git
  GIT_TAG v1.0.27
)

FetchContent_MakeAvailable(LibusbCmake)

target_link_libraries(${PROJECT_NAME} PRIVATE libusb::libusb)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants