You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Windows项目集成FEIG NFC扫描设备遇LNK2019链接错误

解决Flutter FFI调用FEIG库时的LNK2019未解析外部符号错误

我需要在Windows平台的Flutter项目中实现FEIG设备的NFC扫描,采用Flutter FFI方案:通过C++调用FEIG库方法,用CMake编译成DLL后,在Dart中调用DLL接口。目前验证设备USB连接的演示项目编译时出现LNK2019错误,寻求解决方法。


项目目录结构

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.cpp

#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.dart

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()}'),
        ),
      ),
    );
  }
}

编译错误信息

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]

解决方法

1. 修正CMake库链接写法

Windows下链接库时,无需指定完整路径,利用link_directories已指定的搜索路径,直接写库名即可:
修改target_link_libraries为:

target_link_libraries(MyDLL PRIVATE feiglib)

2. 验证FEIG库的符号与架构匹配

  • 使用Visual Studio的dumpbin工具检查feiglib.lib是否包含目标符号:
    dumpbin /symbols ../lib/release/feiglib.lib | findstr "?reader@Manager@FEDM@@SA_KXZ"
    
    无输出则说明库中不存在该符号,需确认FEIG库版本或方法调用是否正确。
  • 确保feiglib.lib的架构(x86/x64)、编译模式(Debug/Release)与Flutter项目完全一致,Flutter默认编译x64架构。

3. 匹配方法签名与返回值类型

错误信息显示Manager::reader()返回unsigned __int64,但当前C++代码返回int,类型不匹配会导致符号解析失败:

  • 修改C++代码:
    #include <cstdint>
    // ...
    uint64_t scanUSB()
    { 
        return Manager::reader();
    }
    
  • 同步修改Dart中的类型定义:
    typedef AddFunc = Uint64 Function();
    typedef Add = int Function();
    

4. 确认库的链接类型

检查Manager.h中reader()的声明:

  • 如果FEIG是动态库,声明需带有__declspec(dllimport);
  • 如果是静态库,需移除__declspec(dllimport),确保头文件声明与库类型匹配。

内容的提问来源于stack exchange,提问作者Sineks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 19:46:18