現在、Flutter プロジェクトで Linux デスクトップ アプリケーションの C++ 関数を呼び出す必要があります (dart:ffi を使用)。このページhttps://flutter.dev/docs/development/platform-integration/c-interop#first-party-libraryでは、Linux でこの種のプロジェクトを構成する方法について説明していません (Windows の場合もありません)。
何度か試してみましたが、C++ ライブラリを正しくリンクできません。
C++ 関数
#include<iostream>
extern "C" {
void do_something(const char *program_name, const char *password)
{
//Do something with data
}
}
CMakeLists.txt に次の行を追加しました。
add_library(my_native STATIC ../native_lib/my_native.cpp)
target_link_libraries(${BINARY_NAME} PUBLIC my_native)
最後に、次の方法で Dart にリンクしています。
// Since the CMake code was added in the executable CMakeLists.txt, it seems that it
// is supposed to be done that way, with DynamicLibrary.executable() rather than DynamicLibrary.process()
// method
final DynamicLibrary lib = DynamicLibrary.executable();
final doSomethingFuncPointer = lib.lookup<NativeFunction<do_something_signature>>("do_something");
正常にコンパイルされますが、起動時にプログラムは次のエラーを返します。
[ERROR:flutter/lib/ui/ui_dart_state.cc(171)] Unhandled Exception: Invalid argument(s): Failed to lookup symbol (/home/me/Documents/flutter/desktop_installer_framework/build/linux/debug/bundle/installer: undefined symbol: do_something)
また、動的リンクも試しました (ライブラリをSHARED
CMakeLists.txt のようにマークし、.xml でリンクしますDynamicLibrary.open("libmy_native.so")
)。またDynamicLibrary.process()
、2番目のCMakeLists.txt(linux/flutterにあるもの)内にCMake行を呼び出して配置しようとしました。シンボルを見つけることはありません。だから、私はここで何かが欠けていると思います。どんな助けでも大歓迎です。
よろしくお願いします