• Khi chạy Flutter app dùng FFI trên iOS, nhiều dev gặp lỗi:
Invalid argument(s): Failed to lookup symbol 'sine_wave_init':
dlsym(RTLD_DEFAULT, sine_wave_init): symbol not found

🔍 Nguyên nhân
Trên iOS, khi bạn dùng:

DynamicLibrary.process()

thì các hàm C phải được export từ executable. Tuy nhiên Xcode mặc định không export symbol, dẫn đến FFI không tìm thấy function.

  • Cách sửa nhanh
  1. Export symbol trong file header
    Thêm visibility attribute vào các function C:
#define FFI_EXPORT __attribute__((visibility("default")))

FFI_EXPORT SineWaveAudioContext* sine_wave_init(...);
FFI_EXPORT int sine_wave_start(...);
FFI_EXPORT int sine_wave_stop(...);
...
  1. Tạo exported_symbols.txt
    Tạo file ios/Runner/exported_symbols.txt:
_sine_wave_init
_sine_wave_start
_sine_wave_stop
_sine_wave_set_frequency
_sine_wave_set_amplitude
_sine_wave_is_playing
_sine_wave_cleanup

(Lưu ý: phải có dấu gạch dưới _)

  1. Thêm Linker Flags vào Xcode
    Trong Build Settings → Other Linker Flags, thêm:
-exported_symbols_list
$(SRCROOT)/Runner/exported_symbols.txt

Áp dụng cho cả Debug và Release.

  1. Clean & rebuild iOS
flutter clean
flutter build ios --release
  1. Kết quả
    Sau khi export đúng symbol, Flutter FFI sẽ load được các hàm C khi gọi:
DynamicLibrary.process()

và app chạy bình thường trên iOS thật hoặc Simulator.