Philiste
Philiste

Reputation: 171

Dart / Flutter ffi on linux - Problem to configure CMake

I currently need to call a C++ function in my Flutter project, for a Linux desktop application (using dart:ffi). This page https://flutter.dev/docs/development/platform-integration/c-interop#first-party-library doesn't explain how to configure this kind of projects on Linux (neither for Windows).

After a few try, I'm unable to correctly link the C++ library.

The C++ Function

#include<iostream>
extern "C" {
void do_something(const char *program_name, const char *password)
{
 //Do something with data 

}
}

I added the following lines to the CMakeLists.txt :

add_library(my_native STATIC ../native_lib/my_native.cpp)
target_link_libraries(${BINARY_NAME} PUBLIC my_native)

And finally, I'm linking in Dart the following way :

// 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");

It compiles fine, but at startup, the program returns the following error :

[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)

I also tried with dynamic linking (marking the library as SHARED in CMakeLists.txt and linking it with DynamicLibrary.open("libmy_native.so")). Also tried calling DynamicLibrary.process() and putting the CMake lines inside the second CMakeLists.txt (the one in linux/flutter). It never finds symbols. So, I think I'm missing something here. Any help would be appreciated.
Best regards

Upvotes: 1

Views: 1238

Answers (1)

smorgan
smorgan

Reputation: 21599

You've shown the definition; is that also the declaration? If so (which seems likely since you have the extern there) you are missing the visibilty and used annotations that the page you linked to described needing for C++, which would explain why the symbol isn't in the resulting binary.

Upvotes: 2

Related Questions