rigexpert / libfobos

Fobos SDR API library. Full source code.
GNU Lesser General Public License v2.1
6 stars 4 forks source link

macOS build #2

Closed mvmn closed 2 months ago

mvmn commented 4 months ago

Any plans for making a macOS build instructions?

mvmn commented 3 months ago

P.S. I've tried to build on macOS, but I get errors with regard to libusb. I have libusb installed via Homebrew in /Users/mvmn/homebrew/Cellar/libusb/1.0.27

/Users/mvmn/homebrew/Cellar/libusb/
/Users/mvmn/homebrew/Cellar/libusb//1.0.27
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/INSTALL_RECEIPT.json
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/.brew
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/.brew/libusb.rb
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/ChangeLog
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/AUTHORS
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/include
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/include/libusb-1.0
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/include/libusb-1.0/libusb.h
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/README
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/TODO
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/COPYING
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/NEWS
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/lib
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/lib/pkgconfig
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/lib/pkgconfig/libusb-1.0.pc
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/lib/libusb-1.0.dylib
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/lib/libusb-1.0.0.dylib
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/lib/libusb-1.0.a
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/sam3u_benchmark.c
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/ezusb.h
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/fxload.c
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/ezusb.c
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/xusb.c
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/dpfp.c
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/testlibusb.c
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/hotplugtest.c
/Users/mvmn/homebrew/Cellar/libusb//1.0.27/share/libusb/examples/listdevs.c

I've tried to compile libfobos with these additions to CMakeLists.txt

# Specify the paths to libusb
set(LIBUSB_INCLUDE_DIRS "/Users/mvmn/homebrew/Cellar/libusb/1.0.27/include")
set(LIBUSB_LIBRARY_DIRS "/Users/mvmn/homebrew/Cellar/libusb/1.0.27/lib")
set(LIBUSB_LIBRARIES "/Users/mvmn/homebrew/Cellar/libusb/1.0.27/lib")

include_directories(${LIBUSB_INCLUDE_DIRS})
link_directories(${LIBUSB_LIBRARY_DIRS})

But it fails with errors:

-- The C compiler identification is AppleClang 15.0.0.15000309
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
>>>CMAKE_INSTALL_LIBDIR: lib
-- Build type not specified: defaulting to release.
-- Found PkgConfig: /Users/mvmn/homebrew/bin/pkg-config (found version "0.29.2")
-- Checking for module 'libusb-1.0'
--   Found libusb-1.0, version 1.0.27
>>>Build: Release
>>>OS: Darwin
>>>VS Platform:
>>>LIBUSB_LIBRARIES: /Users/mvmn/homebrew/Cellar/libusb/1.0.27/lib
>>>LIBUSB_INCLUDE_DIRS: /Users/mvmn/homebrew/Cellar/libusb/1.0.27/include
>>>PROJECT_BINARY_DIR: /Users/mvmn/Downloads/libfobos/build
-- Configuring done (1.0s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/mvmn/Downloads/libfobos/build
[ 14%] Building C object CMakeFiles/libfobos.dir/fobos/fobos.c.o
/Users/mvmn/Downloads/libfobos/fobos/fobos.c:1805:34: warning: expression result unused [-Wunused-value]
            printf_internal("E");-
                                 ^
1 warning generated.
[ 28%] Linking C shared library libfobos.dylib
Undefined symbols for architecture x86_64:
  "_libusb_alloc_transfer", referenced from:
      _fobos_alloc_buffers in fobos.c.o
  "_libusb_bulk_transfer", referenced from:
      _fobos_rx_start_sync in fobos.c.o
      _fobos_rx_start_sync in fobos.c.o
      _fobos_rx_start_sync in fobos.c.o
      _fobos_rx_start_sync in fobos.c.o
      _fobos_rx_read_sync in fobos.c.o
  "_libusb_cancel_transfer", referenced from:
      _fobos_rx_read_async in fobos.c.o
  "_libusb_claim_interface", referenced from:
      _fobos_rx_open in fobos.c.o
  "_libusb_close", referenced from:
      _fobos_rx_list_devices in fobos.c.o
      _fobos_rx_open in fobos.c.o
      _fobos_rx_close in fobos.c.o
  "_libusb_control_transfer", referenced from:
      _fobos_spi in fobos.c.o
      _fobos_spi in fobos.c.o
      _fobos_i2c_transfer in fobos.c.o
      _fobos_i2c_transfer in fobos.c.o
      _fobos_i2c_write in fobos.c.o
      _fobos_i2c_read in fobos.c.o
      _fobos_max2830_write_reg in fobos.c.o
      ...
  "_libusb_exit", referenced from:
      _fobos_rx_get_device_count in fobos.c.o
      _fobos_rx_list_devices in fobos.c.o
      _fobos_rx_open in fobos.c.o
      _fobos_rx_close in fobos.c.o
  "_libusb_free_device_list", referenced from:
      _fobos_rx_get_device_count in fobos.c.o
      _fobos_rx_list_devices in fobos.c.o
      _fobos_rx_open in fobos.c.o
  "_libusb_free_transfer", referenced from:
      _fobos_free_buffers in fobos.c.o
  "_libusb_get_device_descriptor", referenced from:
      _fobos_rx_get_device_count in fobos.c.o
      _fobos_rx_list_devices in fobos.c.o
      _fobos_rx_open in fobos.c.o
  "_libusb_get_device_list", referenced from:
      _fobos_rx_get_device_count in fobos.c.o
      _fobos_rx_list_devices in fobos.c.o
      _fobos_rx_open in fobos.c.o
  "_libusb_get_string_descriptor_ascii", referenced from:
      _fobos_rx_list_devices in fobos.c.o
      _fobos_rx_open in fobos.c.o
      _fobos_rx_open in fobos.c.o
      _fobos_rx_open in fobos.c.o
  "_libusb_handle_events_timeout_completed", referenced from:
      _fobos_rx_read_async in fobos.c.o
      _fobos_rx_read_async in fobos.c.o
      _fobos_rx_read_async in fobos.c.o
  "_libusb_init", referenced from:
      _fobos_rx_get_device_count in fobos.c.o
      _fobos_rx_list_devices in fobos.c.o
      _fobos_rx_open in fobos.c.o
  "_libusb_open", referenced from:
      _fobos_rx_list_devices in fobos.c.o
      _fobos_rx_open in fobos.c.o
  "_libusb_submit_transfer", referenced from:
      _fobos_rx_read_async in fobos.c.o
      __libusb_callback in fobos.c.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]: *** [libfobos.dylib] Error 1
make[1]: *** [CMakeFiles/libfobos.dir/all] Error 2
make: *** [all] Error 2

Can anyone help me with this?

thereisnocat commented 2 months ago

I was able to get libfobos to build on the Mac by compiling with gcc instead of clang. You need to install gcc with homebrew, then set the C++ compiler when running cmake and then again when running make:

cmake -D CMAKE_CXX_COMPILER=/opt/homebrew/bin/g++-14 .. make CXX=/opt/homebrew/bin/g++-14

That should get you most of the way there. I think there was a problem in one of the Homebrew modules that caused the build to fail once I got to this point, plus I had to manually edit CMakeLists.txt to change the version of C from c99 to gnu17 and to fix a broken import in fobos/fobos.c where it couldn’t find libusb-1.0/libusb.h, but pointing to just libusb.h worked fine.

mvmn commented 2 months ago

I have gcc, I've tried

cmake .. -DCMAKE_CXX_COMPILER=$(which g++-14)
make CXX=$(which g++-14)

but to no avail - exactly the same errors occur

thereisnocat commented 2 months ago

If it’s still throwing the error mentioning clang, then it’s not honoring the -D; maybe try that before the ..

mvmn commented 2 months ago

Seems like I have a wrong gcc

> gcc -v
Apple clang version 15.0.0 (clang-1500.3.9.4)
mvmn commented 2 months ago

Ok, seem this did some change

cmake -DCMAKE_C_COMPILER=$(which gcc) -DCMAKE_CXX_COMPILER=$(which g++-14) ..

even though in logs it still says it's CLang

But in the end I still have same errors

P.S. Installing gcc via Homebrew - seems the one I've been using is macOS bundled

thereisnocat commented 2 months ago

You have to specify gcc-version, like gcc-14, because Apple has gcc as an alias for clang.

mvmn commented 2 months ago

@thereisnocat thanks a ton, I was able to build it all successfully using same steps as you mentioned.

Just in case, here are the steps in detail:

mvmn commented 2 months ago

Resolved by using Homebrew gcc