sansuido / sdl2

BSD 3-Clause "New" or "Revised" License
21 stars 3 forks source link

Failed to load dynamic library 'libSDL2.dylib' on MacOS #6

Open damywise opened 1 year ago

damywise commented 1 year ago

Code:

if (sdlInit(SDL_INIT_VIDEO) != 0) {
    print(sdlGetError());
}

Logs:

Unhandled exception:
Invalid argument(s): Failed to load dynamic library 'libSDL2.dylib': dlopen(libSDL2.dylib, 0x0001): tried: 'libSDL2.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibSDL2.dylib' (no such file), '/Users/USER1234/Downloads/flutter/bin/cache/dart-sdk/bin/./libSDL2.dylib' (no such file), '/Users/USER1234/Downloads/flutter/bin/cache/dart-sdk/bin/../../../libSDL2.dylib' (no such file), '/Users/USER1234/Downloads/flutter/bin/cache/dart-sdk/bin/Frameworks/libSDL2.dylib' (no such file), '/Users/USER1234/Downloads/flutter/bin/cache/dart-sdk/bin/./libSDL2.dylib' (no such file), '/Users/USER1234/Downloads/flutter/bin/cache/dart-sdk/bin/../../../libSDL2.dylib' (no such file), '/Users/USER1234/Downloads/flutter/bin/cache/dart-sdk/bin/Frameworks/libSDL2.dylib' (no such file), '/usr/lib/libSDL2.dylib' (no such file, not in dyld cache), 'libSDL2.dylib' (no such file), '/usr/lib/libSDL2.dylib' (no such file, not in dyld cache)
#0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:43)
#1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
#2      dylibOpen (package:sdl2/sdl2/dylib.dart:25:25)
#3      libSdl2 (package:sdl2/sdl2/generated/lib_sdl.dart:7:23)
#4      libSdl2 (package:sdl2/sdl2/generated/lib_sdl.dart)
#5      sdlInit (package:sdl2/sdl2/generated/lib_sdl.dart:174:33)
#6      startRender (package:sdl_test/sdl_test.dart:4:7)
#7      main (file:///Users/USER1234/Documents/GitHub/sdl_test/bin/sdl_test.dart:4:12)
#8      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:33)
#9      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)

Flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.7.11, on macOS 13.2.1 22D68 darwin-arm64, locale
    en-ID)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] Android Studio (version 2021.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2022.3.2)
[✓] VS Code (version 1.77.2)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

• No issues found!
sansuido commented 1 year ago

Hi, damywise! I don't have MacOS so I can't test it, but I think you can use it by installing the static library (libsdl2.dylib and more) on MacOS.

You can do it by using Homebrew.

brew update brew upgrade brew doctor

brew install sdl2 brew install sdl2_image brew install sdl2_ttf brew install sdl2_mixer brew install sdl2_net

damywise commented 1 year ago

Hello there, sorry it took so long, but I have tried installing them and it's still not working. The error is still the same

damywise commented 1 year ago

Might be related: https://github.com/Vladar4/sdl2_nim/issues/41 https://github.com/sanette/oplot/issues/2

damywise commented 1 year ago

Good news, I've finally found a fix that loads the sdl2 library: modify dylib.dart as follows, inspired by dart_sdl package

//...
    case 'macos':
      header = 'lib';
      extension = '.dylib';

      if (!File(header + mark + extension).existsSync()) {
        header = '';
        extension = '.framework/$mark';
      }
      break;
 //...

Bad news is, it still doesn't work. Error at this specific line https://github.com/sansuido/sdl2/blob/bbce1c6ed5af456812c0cc557f6c0ef28f3f5dd0/example/main.dart#L16-L20 (Almost same error as dart_sdl, though the audio example in dart_sdl works):

NSWindow drag regions should only be invalidated on the Main Thread!
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 12783.

===== CRASH =====
si_signo=Trace/BPT trap: 5(5), si_code=0, si_addr=0x1921d9b94
version=3.1.1 (stable) (Tue Sep 5 12:20:14 2023 +0000) on "macos_arm64"
pid=53081, thread=11523, isolate_group=(nil)(0x0), isolate=(nil)(0x0)
os=macos, arch=arm64, comp=no, sim=no
isolate_instructions=0, vm_instructions=102cde540
fp=16dae6950, sp=16dae6920, pc=1921d9b94
  pc 0x00000001921d9b94 fp 0x000000016dae6950 __abort+0xa0
  pc 0x833e8001921d9af4 fp 0x000000016dae6990 __abort+0x833e800000000000
  pc 0x0f060001921d8e44 fp 0x000000016dae69f0 err+0xf06000000000000
  pc 0xea6b8001956793c4 fp 0x000000016dae6a10 NSViewRootLayerBack+0xea6b800000000000
  pc 0xd5788001956792f4 fp 0x000000016dae6a60 NSViewBuildLayerTreeForDisplay+0xd578800000000098
  pc 0x8f2f000195da56e0 fp 0x000000016dae6ae0 NSWindowUpdateLayerTree+0x8f2f000000000050
  pc 0x8545800195677ae0 fp 0x000000016dae6b20 -[NSWindow displayIfNeeded]+0x85458000000000cc
  pc 0x4c58800195677988 fp 0x000000016dae6b80 __NSWindowGetDisplayCycleObserverForDisplay_block_invoke+0x4c58800000000204
  pc 0x0a57800195676868 fp 0x000000016dae6bc0 NSDisplayCycleObserverInvoke+0xa578000000000a8
  pc 0x9c2e0001956764c4 fp 0x000000016dae6d50 NSDisplayCycleFlush+0x9c2e000000000284
  pc 0x8f35800199894b6c fp 0x000000016dae6d80 CA::Transaction::run_commit_handlers(CATransactionPhase)+0x8f35800000000078
  pc 0xb538800199893928 fp 0x000000016dae6e60 CA::Transaction::commit()+0xb538800000000140
  pc 0xfd6c0001998aed48 fp 0x000000016dae6f30 CA::Transaction::release_thread(void*)+0xfd6c0000000000e4
  pc 0xbb448001922c9970 fp 0x000000016dae6f90 _pthread_tsd_cleanup+0xbb4480000000026c
  pc 0xbb6b0001922cc69c fp 0x000000016dae6fc0 _pthread_exit+0xbb6b000000000054
  pc 0x7f6b8001922cbfb4 fp 0x000000016dae6fe0 _pthread_start+0x7f6b8000000000a0
-- End of DumpStackTrace

===== CRASH =====
si_signo=Trace/BPT trap: 5(5), si_code=0, si_addr=0x1921d9b94
Aborting reentrant request for stack trace.

Exited.
damywise commented 1 year ago

Also I made a fork in case anyone wants to test it out https://github.com/damywise/sdl2. If it the libraries doesn't load, just put the *.framework folders to one of the paths in the error