I need to create a Flutter project for windows that can use a FEIG device for NFC scanning. The only solution I have found so far is to use Flutter FFI (Foreign Function Interface) to compile C++ files and call their methods in Dart. The C++ files will call methods from the Feig libraries, and a CMakeLists.txt file will be used to compile the C++ code into a DLL (Dynamic Link Library). Flutter FFI can then read the methods from the DLL, allowing me to access and use FEIG classes and methods in my Flutter application.
This is a demo where I want to check if the Feig device is connected to the USB port using
Directory Struct:
flutter_feig_app_demo_1/
├── build/
│ ├── CMakesLists.txt
│ ├── cpp_lib.cpp
| ├── MyDLL.vcxproj
| └── Release/
| └── MyDLL.dll
└── lib/
├── main.dart
├── native_lib.dart
├── lib
| └── release
| └── feiglib.lib
└── include
└── fedm
├── Reader.h
└── Manager.h
CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(flutter_feig_app_demo_1 LANGUAGES CXX)
set(SOURCES cpp_lib.cpp)
include_directories(../lib/include/fedm)
# Specify the library directories
link_directories(../lib/release)
# Add the shared library (DLL)
add_library(MyDLL SHARED ${SOURCES})
target_link_libraries(MyDLL PRIVATE ../lib/release/feiglib)
# Set the output directory for the DLL
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Enable Windows export symbols
if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
cpp_lib file:
#include <iostream>
#include "Reader.h"
#include "Manager.h"
extern "C"
{
using namespace FEDM;
using namespace std;
int scanUSB()
{
return Manager::reader();
}
}
native_lib.dart
import 'dart:ffi';
import 'dart:io';
final DynamicLibrary nativelib = DynamicLibrary.open("lib/build/Release/MyDLL.dll");
typedef AddFunc = Int32 Function();
typedef Add = int Function();
final Add add = nativelib.lookupFunction<AddFunc,Add>('scanUSB');
int isDiscovered(){
return add();
}
main:
import 'package:flutter/material.dart';
import 'native_lib.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text('Hello World! ${isDiscovered()}'),
),
),
);
}
}
Error:
cpp_lib.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static unsigned __int64 __cdecl FEDM::Manager::reader(void)" (__imp_?reader@Manager@FEDM@@SA_KXZ) referenced in function scanUSB [root\build\MyDLL.vcxproj]
root\build\Release\MyDLL.dll : fatal error LNK1120: 1 unresolved externals [root/build\MyDLL.vcxproj]