Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

cmake compiler directive for multi os



  • I wrote some code, but i am new to cmake, and i want it to compile on different os, in windows i have a library in a different directory.

    if (WIN32)
       target_link_libraries(USB_GUI PRIVATE Qt${QT_VERSION_MAJOR}::Widgets "D:/msys2/mingw64/lib/libusb-1.0.a")
       set(OS 0)
    endif(WIN32)
    
    if (APPLE)
        target_link_libraries(USB_GUI PRIVATE Qt${QT_VERSION_MAJOR}::Widgets "/usr/local/Cellar/libusb/1.0.24/lib/libusb-1.0.a")
        set(OS 1)
    endif(APPLE)
    
    
    

    inside my c code i have

    
    #include <stdio.h>
    #include <string.h>
    
    #if (OS==0)
    #include "D:\msys2\mingw64\include\libusb-1.0\libusb.h"
    #endif
    #if (OS==1)
    #include "/usr/local/Cellar/libusb/1.0.24/include/libusb-1.0/libusb.h"
    #endif
    

    I get the following although i compile on mac, any help is appreciated

    /Volumes/DATA/Activate_Software/USBGUI/USB_GUI/mainwindow.h:8: error: 'D:\msys2\mingw64\include\libusb-1.0\libusb.h' file not found
    #include "D:\msys2\mingw64\include\libusb-1.0\libusb.h"
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

  • Lifetime Qt Champion

    Hi,

    The backslash is an escape sequence in strings so it won't work.

    However, you are doing it wrong. You should only include libusb.h and in your cmake file properly setup the include paths to your libraries includes.



  • could you please teach me how to do it ?



  • i changed it into this

    if (WIN32)
       target_link_libraries(USB_GUI PRIVATE Qt${QT_VERSION_MAJOR}::Widgets "D:/dmsys2/mingw64/lib/libusb-1.0.a")
       add_definitions(-D WinX)
    endif(WIN32)
    
    if (APPLE)
        include_directories("/usr/local/Cellar/libusb/1.0.24/include/libusb-1.0")
        target_link_libraries(USB_GUI PRIVATE Qt${QT_VERSION_MAJOR}::Widgets "/usr/local/Cellar/libusb/1.0.24/lib/libusb-1.0.a")
        add_definitions(-D MacX)
    endif(APPLE)
    
    #include "libusb.h"
    

    now i get

    01:49:39: Running steps for project USB_GUI...
    01:49:39: Running CMake in preparation to build...
    01:49:41: Starting: "/usr/local/bin/cmake" --build . --target all
    [ 20%] Automatic MOC and UIC for target USB_GUI
    [ 20%] Built target USB_GUI_autogen
    Scanning dependencies of target USB_GUI
    [ 40%] Building CXX object CMakeFiles/USB_GUI.dir/USB_GUI_autogen/mocs_compilation.cpp.o
    [ 60%] Building CXX object CMakeFiles/USB_GUI.dir/main.cpp.o
    [ 80%] Building CXX object CMakeFiles/USB_GUI.dir/mainwindow.cpp.o
    [100%] Linking CXX executable USB_GUI
    Undefined symbols for architecture x86_64:
      "_CFDataGetBytes", referenced from:
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
      "_CFDataGetLength", referenced from:
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
      "_CFDataGetTypeID", referenced from:
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
      "_CFGetTypeID", referenced from:
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
          _get_ioregistry_value_number in libusb-1.0.a(darwin_usb.o)
      "_CFNumberGetTypeID", referenced from:
          _get_ioregistry_value_number in libusb-1.0.a(darwin_usb.o)
      "_CFNumberGetValue", referenced from:
          _get_ioregistry_value_number in libusb-1.0.a(darwin_usb.o)
      "_CFRelease", referenced from:
          _darwin_close in libusb-1.0.a(darwin_usb.o)
          _darwin_release_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_kernel_driver_active in libusb-1.0.a(darwin_usb.o)
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
          _get_ioregistry_value_number in libusb-1.0.a(darwin_usb.o)
      "_CFRetain", referenced from:
          _darwin_open in libusb-1.0.a(darwin_usb.o)
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_CFRunLoopAddSource", referenced from:
          _darwin_open in libusb-1.0.a(darwin_usb.o)
          _darwin_claim_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_CFRunLoopGetCurrent", referenced from:
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_CFRunLoopRemoveSource", referenced from:
          _darwin_close in libusb-1.0.a(darwin_usb.o)
          _darwin_release_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_CFRunLoopRun", referenced from:
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_CFRunLoopSourceCreate", referenced from:
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_CFRunLoopSourceSignal", referenced from:
          _darwin_exit in libusb-1.0.a(darwin_usb.o)
      "_CFRunLoopStop", referenced from:
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_CFRunLoopWakeUp", referenced from:
          _darwin_exit in libusb-1.0.a(darwin_usb.o)
      "_CFUUIDGetConstantUUIDWithBytes", referenced from:
          _darwin_claim_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
      "_CFUUIDGetUUIDBytes", referenced from:
          _darwin_claim_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
      "_IOCreatePlugInInterfaceForService", referenced from:
          _darwin_claim_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
      "_IOIteratorNext", referenced from:
          _darwin_init in libusb-1.0.a(darwin_usb.o)
          _process_new_device in libusb-1.0.a(darwin_usb.o)
          _darwin_devices_detached in libusb-1.0.a(darwin_usb.o)
          _darwin_devices_attached in libusb-1.0.a(darwin_usb.o)
          _darwin_clear_iterator in libusb-1.0.a(darwin_usb.o)
          _darwin_get_interface in libusb-1.0.a(darwin_usb.o)
      "_IOKitWaitQuiet", referenced from:
          _darwin_hotplug_poll in libusb-1.0.a(darwin_usb.o)
      "_IONotificationPortCreate", referenced from:
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_IONotificationPortDestroy", referenced from:
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_IONotificationPortGetRunLoopSource", referenced from:
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_IOObjectRelease", referenced from:
          _darwin_init in libusb-1.0.a(darwin_usb.o)
          _darwin_claim_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_kernel_driver_active in libusb-1.0.a(darwin_usb.o)
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
          _process_new_device in libusb-1.0.a(darwin_usb.o)
          _darwin_devices_detached in libusb-1.0.a(darwin_usb.o)
          ...
      "_IORegistryEntryCreateCFProperty", referenced from:
          _darwin_kernel_driver_active in libusb-1.0.a(darwin_usb.o)
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
          _get_ioregistry_value_number in libusb-1.0.a(darwin_usb.o)
      "_IORegistryEntryGetParentEntry", referenced from:
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
      "_IOServiceAddMatchingNotification", referenced from:
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_IOServiceGetMatchingServices", referenced from:
          _darwin_init in libusb-1.0.a(darwin_usb.o)
      "_IOServiceMatching", referenced from:
          _darwin_init in libusb-1.0.a(darwin_usb.o)
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "___CFConstantStringClassReference", referenced from:
          CFString in libusb-1.0.a(darwin_usb.o)
          CFString in libusb-1.0.a(darwin_usb.o)
          CFString in libusb-1.0.a(darwin_usb.o)
          CFString in libusb-1.0.a(darwin_usb.o)
          CFString in libusb-1.0.a(darwin_usb.o)
          CFString in libusb-1.0.a(darwin_usb.o)
      "_kCFAllocatorDefault", referenced from:
          _darwin_kernel_driver_active in libusb-1.0.a(darwin_usb.o)
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
          _get_ioregistry_value_number in libusb-1.0.a(darwin_usb.o)
      "_kCFAllocatorSystemDefault", referenced from:
          _darwin_claim_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_get_cached_device in libusb-1.0.a(darwin_usb.o)
      "_kCFRunLoopCommonModes", referenced from:
          _darwin_open in libusb-1.0.a(darwin_usb.o)
      "_kCFRunLoopDefaultMode", referenced from:
          _darwin_close in libusb-1.0.a(darwin_usb.o)
          _darwin_claim_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_release_interface in libusb-1.0.a(darwin_usb.o)
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
      "_kIOMasterPortDefault", referenced from:
          _darwin_init in libusb-1.0.a(darwin_usb.o)
          _darwin_hotplug_poll in libusb-1.0.a(darwin_usb.o)
          _darwin_event_thread_main in libusb-1.0.a(darwin_usb.o)
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[2]: *** [USB_GUI] Error 1
    make[1]: *** [CMakeFiles/USB_GUI.dir/all] Error 2
    make: *** [all] Error 2
    01:49:46: The process "/usr/local/bin/cmake" exited with code 2.
    Error while building/deploying project USB_GUI (kit: Desktop Qt 5.12.1 Cmake)
    When executing step "CMake Build"
    01:49:46: Elapsed time: 00:07.
    

  • Lifetime Qt Champion

    @SherifOmran said in cmake compiler directive for multi os:

    IONotificationPortGetRunLoopSource

    You need to link against the IOKit framework.


Log in to reply