Include DLL in `flutter build`

2,106 views
Skip to first unread message

Chris Norman

unread,
Oct 12, 2021, 2:10:17 PM10/12/21
to mi...@dartlang.org
Hi all,
I'm currently playing with Flutter for desktop, and I'd like to include a DLL with a release.

I know I need the dll alongside the eventual EXE, which is fine, but is there a way to make cmake put it there for me without me having to make a build script?

I've been googling to try and statically link the C++ code I'm using, but that doesn't seem to work, so now I'm trying a seemingly more simple step of having the DLL shipped with the application.

Thanks in advance,

Take care,

Chris Norman

Daco Harkes

unread,
Oct 12, 2021, 2:38:35 PM10/12/21
to mi...@dartlang.org
Hey Chris,

This use case is supported by creating a plugin with flutter create -t plugin <name> and using the <name>_bundled_libraries field in the generated <name>\windows\CMakeLists.txt

# List of absolute paths to libraries that should be bundled with the plugin
set(<name>_bundled_libraries
"${CMAKE_CURRENT_SOURCE_DIR}/../native/lib/windows_x64/mylib_dylib.dll;${CMAKE_CURRENT_SOURCE_DIR}/../native/lib/windows_x64/mylib_dylib_dependency.dll"
PARENT_SCOPE
)
Note that I've put the precompiled libraries in <name>/native/lib/windows_x64 here.

If you're building from source, and you're willing to have the source code in a plugin project with it's own CMake build. Then you can invoke that build, and trigger the copy by copying it from the built plugin folder
# Invoke the build of mylib_source.
#
# Note that Flutter uses the Visual Studio compiler so the output dll will
# be in the Debug/ folder of the plugin.
include(${CMAKE_CURRENT_SOURCE_DIR}/../src/CMakeLists.txt)

# List of absolute paths to libraries that should be bundled with the plugin
set(mylib_source_bundled_libraries
"plugins/mylib_source/Debug/mylib_source.dll"
PARENT_SCOPE
)

A plugin project with a precompiled static library also works, but requires modification of the generated Flutter CMake file: flutter_app\linux\CMakeLists.txt
# Generated plugin build rules, which manage building the plugins and adding
# them to the application.
include(flutter/generated_plugins.cmake)

# Link static libraries from plugins into main executable with support for
# looking up symbols with dlsym. (Prefer using dynamic libraries over
# static libraries!)
foreach(plugin ${FLUTTER_PLUGIN_LIST})
if(${plugin}_linked_static_libraries)
# TODO(dacoharkes): Make it work for multiple static libraries.
target_link_libraries(
${BINARY_NAME}
PUBLIC
"${${plugin}_linked_static_libraries}"
)
set_target_properties(
${BINARY_NAME}
PROPERTIES
LINK_FLAGS
"/WHOLEARCHIVE:${plugin}"
)
endif()
endforeach(plugin)

And then you populate that variable in the plugin project again:
set(mylib_staticlib_linked_static_libraries
"${CMAKE_CURRENT_SOURCE_DIR}/../native/lib/windows_x64/mylib_staticlib.lib"
PARENT_SCOPE
)
This is slightly messy because it exposes all symbols. Without exposing all symbols the linker strips them away because no other C/C++ refers to them. So, you're better of using a dynamic library.

I'm in the process of adding these workflows to a new flutter create template. (Something "ffi plugin".)

I have not designed a workflow yet for directly embedding native code in an app without a plugin project. However, you should be able to achieve it by copying and modifying
if(PLUGIN_BUNDLED_LIBRARIES)
install(FILES "${PLUGIN_BUNDLED_LIBRARIES}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
endif()
from flutter_app\windows\CMakeLists.txt and pointing it directly to your dynamic library.

Kind regards,

 •  Daco Harkes
 •  Software Engineer
 •  dacoh...@google.com 
 •  🔵 Blue Dot Ally


--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CANjhqb98uQ7tKEs4VmwEKHL6KeBgm6mB6-zA1h0-5OWGv4F0ow%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages