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.