Integrating un4seen BASS with Flutter using FFI and getting symbol error - flutter

I'm trying to integrate the BASS audio library from un4seen Developments with Flutter, Google's cross-platform app development environment. I've started a Flutter plugin that can be found here: https://github.com/JimTompkins/flutter_bass. It uses Flutter's FFI (foreign function interface) mechanism and a package called ffigen to convert the bass.h file to generated_bindings.dart.
Here is a snippet from generated_bindings.dart:
int BASS_Init(
int device,
int freq,
int flags,
ffi.Pointer<ffi.Void> win,
ffi.Pointer<ffi.Void> dsguid,
) {
return _BASS_Init(
device,
freq,
flags,
win,
dsguid,
);
}
late final _BASS_InitPtr = _lookup<
ffi.NativeFunction<
BOOL Function(ffi.Int32, DWORD, DWORD, ffi.Pointer<ffi.Void>,
ffi.Pointer<ffi.Void>)>>('_BASS_Init');
late final _BASS_Init = _BASS_InitPtr.asFunction<
int Function(
int, int, int, ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Void>)>();
It builds (Flutter 3.3, XCode 13) but throws a run-time error when I try to call the BASS_init function:
Failed to lookup symbol '_BASS_Init': dlsym(0x100f61e18, _BASS_Init): symbol not found
I can see the symbol in the libbass.a file using nm -gU libbass.a
Any comments/suggestions welcomed!
Update 2022-09-20:
I added the following frameworks to the Xcode build:
AVFoundation, AudioToolbox, SystemConfiguration, CFNetwork, Accelerate
In Xcode by going to Build Phase-->Link Binary with Libraries-->Add
This did not make any difference i.e. still getting symbol error
I checked that “Enable bitcode” is set to “No” in Xcode under Build Settings-->Build Options.
The search continues...
Updated 2022-09-29:
I got a hint from this Github issue post: use a force_load option to Xcode in the podspec file:
s.pod_target_xcconfig = {'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386', "OTHER_LDFLAGS" => '-force_load ($SRCROOT)/../../../ios/bass24-ios/bass.xcframework/ios-arm64_armv7_armv7s/libbass.a' } s.vendored_libraries = '($SRCROOT)/../../../ios/bass24-ios/bass.xcframework/ios-arm64_armv7_armv7s/libbass.a'
I tried this but now I'm getting a file not found error.
Could not build the precompiled application for the device.
Error (Xcode): File not found:
(/Users/jimtompkins/git/flutter_bass/example/ios/Pods)/../../../ios/bass24-ios/bass.xcframework/ios-arm64_armv7_armv7s/libbass.a
I checked and the file is there:
% ls -als /Users/jimtompkins/git/flutter_bass/example/ios/Pods/../../../ios/bass24-ios/bass.xcframework/ios-arm64_armv7_armv7s/libbass.a
1536 -rw-r--r--# 1 jimtompkins staff 785096 8 Sep 2021 /Users/jimtompkins/git/flutter_bass/example/ios/Pods/../../../ios/bass24-ios/bass.xcframework/ios-arm64_armv7_armv7s/libbass.a
Any hints greatly appreciated!
Update: 2022-09-30: fixed by removing ( ) around the $SRCROOT.
I also found that I needed to add the necessary frameworks to the OTHER_LDFLAGS command in the podspec file. See the Github repo.

Related

Facing a problem to set up the emulator in flutter

I have the following problem.....
Error: Variables must be declared using the keywords 'const', 'final', 'var' or a type name.
Try adding the name of the type of the variable or the keyword 'var'.
int _counter = 0;dsggfsgfh;
^^^^^^^^^
Target kernel_snapshot failed: Exception
FAILURE: Build failed with an exception.
I'm a beginner with Flutter. How can I solve this problem?
Your code has junk words or sentences, check your line ended with dsggfsgfh; which might be added unfortunately. Just remove it this will work.
It's possible that it's a compatibility issue between flutter framework and engine. You can try and:
Upgrade flutter to the latest version
Create a new project
Copy the code from the old project in steps
If you have plugins from the old project, add them one by one as you run the emulator
(It's possible that a certain version of a plugin has a compatibility issue with flutter)

Swift Package Manager (package successfully added, but Module not found)

I'm new in Swift. I want to create iOS app that can connect to PostgreSQL database. First I found library https://github.com/vapor/postgresql.git that should be added to my project via Swift Package Manager. Using tutorial I added required library to my project successfully (File -> Swift Packages -> Add Package Dependency):
list of added packages from SPM
But when I try to import this module into my view controller, Xcode shows error that module is not found:
not found
I tried several times to rebuild my project, created new project just for testing this issue. Also I found information about build phases and added this lib as a dependency:
build phases
But I still get error: "No such module PostgreSQL".
Can anyone help me?
I found solution by myself. I compiled C static library "libpq" (can be found in PostgreSQL sources) and added it to my swift project. Included this library by adding special bridging header file. Finally I got what I wanted.
P.S. If someone what to repeat, he or she should know: static C library must be compiled for iOS device architecture (and also in iOS simulator architecture which differs from iOS device arch.).

Why the app produced by flutter build web sometimes doesn't work?

I have 2 issues that only appear when executing flutter build web.
Sometimes flutter build web fails complaining (wrongly) about types that were not compatible (see below).
Sometimes the build process finishes but then the web app doesn't work: doesn't display anything and there are no messages in the console.
The error I mention is something like this:
% flutter build web
Target dart2js failed: Exception: lib/main.dart:24:31:
Error: A value of type 'ApiUsersRepository' can't be assigned to a variable of type 'UsersRepository'.
- 'ApiUsersRepository' is from 'package:my_app/api_users_repo.dart' ('lib/api_users_repo.dart').
- 'UsersRepository' is from 'lib/users_repo.dart'.
final UsersRepository usersRepository = ApiUsersRepository();
^
Error: Compilation failed.
The app is working in iOS and web when developing.
The solution
I changed all imports of my files like:
import 'package:my_app/users_repo.dart';
To:
import 'users_repo.dart';
More Details
Investigating the error about types, I found this issue, where the important part is this comment: after changing every import to relative format it resolves my problem.
So I did that, and it solved the 2 issues, the compilation error, and the runtime error.
for me I had to remove a package that was corrupted. c:\src\flutter.pub-cache\hosted\pub.dartlang.org\localstorage-4.0.0+1 Apparently, a file had become corrupted by me invertly. I removed the package and did a flutter pub get then recompiled and it worked.

Linking dynamic library within static framework

I am building the static iOS framework upon multiple libraries in Xcode. One of them should be the card.io. I cannot use cocoapods or carthage. So far I imported .frameworks within the .framework and it works pretty well. However card.io uses static library (.a file) with bunch of headers. It works well in dynamic type of .frameworks (or iOS app project) but in static project I get these errors on including of the .a files:
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't locate file for: -lCardIO
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: -lCardIO is not an object file (not allowed in a library)
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't locate file for: -lopencv_core
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: -lopencv_core is not an object file (not allowed in a library)
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't locate file for: -lopencv_imgproc
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: -lopencv_imgproc is not an object file (not allowed in a library)
Regarding these errors I downloaded the source codes of card.io and it looks that there is dynamic .framework target waiting for build. I tried to use this one instead of .a files and headers - so my project at least can be builded. Because the card.io does not contatin architectures for simulator (which, by the way, it should with this release 5.2.2) I am not able to test it in unit tests, so when I test this solution on device I get this error:
dyld: Library not loaded: #rpath/CardIO.framework/CardIO
Referenced from: /var/containers/Bundle/Application/55D3AF7F-83F4-4B3D-A667-0FCO93CCC441/App/AppDemo
Reason: no suitable image found.
So far my knowledge+google+stackoverflow is stuck here, because it looks that xcode does not support the .framework within .framework this way.
To this moment I spent two days with this "issue", so the question is: Does exist any solution for including dynamic library into static framework? Or any solution to include card.io into static framework?
EDIT:
Well actually the solution was more stupid than I would think (as always). Just to include card.io in the .framework go to "Project Description -> Build Settings -> Library search paths" and type the path where should xcode look for the libraries. This approach solves the first one issue of this post - this means the implementation of the .a libraries and headers.
At this moment I cannot guarantee it will work in releases based on my framework so I will update this post to confirm it later. I hope it helps someone...

CoreText compile errors via DTCoreText

In my first experience with CocoaPods I've run into a problem. I'm using CocoaPods on an existing project to install DTCoreText specifically for DTAttributedLabel. All goes smoothly and appears to work fine but the second I include DTCoreText.h I get multiple errors with the CoreText framework like the following:
CoreText.framework/Headers/CTRunDelegate.h:130:16: Expected member name or ';' after declaration specifiers
typedef struct
{
CFIndex version;
CTRunDelegateDeallocateCallback dealloc;
CTRunDelegateGetAscentCallback getAscent;
CTRunDelegateGetDescentCallback getDescent;
CTRunDelegateGetWidthCallback getWidth;
} CTRunDelegateCallbacks;
CoreText.framework/Headers/SFNTLayoutTypes.h:821:23: Expected member name or ';' after declaration specifiers
struct LcarCaretTable {
Fixed version;
UInt16 format;
SFNTLookupTable lookup;
};
I created a single view project just to check my install technique and was able to use DTCoreText just fine.
Is there a setting that I'm missing that could be causing this? (Deployment target: 6.1, SDK 6.1)
Perhaps a conflict with an existing library?
I resolved this issue by creating a new project and installing all dependencies through Cocoapods. It appears the issue was related to a conflict.