我目前需要在我的 Flutter 项目中调用一个 C++ 函数,用于 Linux 桌面应用程序(使用 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 并将其与 链接DynamicLibrary.open("libmy_native.so")
)。还尝试调用DynamicLibrary.process()
CMake 行并将其放入第二个 CMakeLists.txt(linux/flutter 中的那个)。它永远不会找到符号。所以,我想我在这里遗漏了一些东西。任何帮助,将不胜感激。
此致