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

CMake配置Microsoft SEAL库遇链接错误:无法找到函数定义

问题

尝试用CMake配置基于Microsoft SEAL库的FHEProject项目,已编写CMakeLists.txt和测试main.cpp,SEAL的头文件、配置文件及.lib库文件路径均正确,但编译时出现大量undefined reference链接错误。IDE能识别SEAL头文件并给出函数提示,但调用SEAL函数时触发链接错误,无法正常编译运行。

配置文件与代码

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(FHEProject VERSION 0.1.0 LANGUAGES C CXX)
set(HEADER_FILE_DIR D:/SEAL/native/src)
include_directories(${HEADER_FILE_DIR})

list(APPEND CMAKE_PREFIX_PATH D:/SEAL/build/cmake)

find_package(SEAL 4.1 REQUIRED)

add_executable(${PROJECT_NAME} main.cpp) 

target_link_libraries(${PROJECT_NAME} SEAL::seal)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})

main.cpp

#include <iostream>
#include<seal/seal.h>
using namespace std;
using namespace seal;
int main(){
    EncryptionParameters parms(scheme_type::bfv);
    for(int i=0;i<10;i++){
        cout<<i<<"\n";
    }
    return 0;
}

文件路径信息

  • seal.h头文件:D:\SEAL\native\src\seal\seal.h
  • sealconfig.cmake配置文件:D:\SEAL\build\cmake\sealconfig.cmake
  • seal-4.1.lib库文件:D:\SEAL\build\lib\Debug\seal-4.1.lib

编译错误信息

[main] Building folder: pj 
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build d:/pj/build --config Debug --target all -j 10 --
[build] [ 50%] Building CXX object CMakeFiles/FHEProject.dir/main.cpp.obj
[build] [100%] Linking CXX executable FHEProject.exe
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\FHEProject.dir/objects.a(main.cpp.obj):D:/SEAL/native/src/seal/util/globals.h:31:(.text$_ZTWN4seal4util16global_variables15tls_memory_poolE[_ZTWN4seal4util16global_variables15tls_memory_poolE]+0x9): undefined reference to `TLS init function for seal::util::global_variables::tls_memory_pool'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\FHEProject.dir/objects.a(main.cpp.obj): in function `seal::Modulus::Modulus(unsigned long long)':
[build] D:/SEAL/native/src/seal/modulus.h:45:(.text$_ZN4seal7ModulusC1Ey[_ZN4seal7ModulusC1Ey]+0x6a): undefined reference to `seal::Modulus::set_value(unsigned long long)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\FHEProject.dir/objects.a(main.cpp.obj): in function `seal::EncryptionParameters::EncryptionParameters(seal::scheme_type)':
[build] D:/SEAL/native/src/seal/encryptionparams.h:99:(.text$_ZN4seal20EncryptionParametersC1ENS_11scheme_typeE[_ZN4seal20EncryptionParametersC1ENS_11scheme_typeE]+0xbd): undefined reference to `seal::EncryptionParameters::compute_parms_id()'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\FHEProject.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr._ZN4seal13parms_id_zeroE[.refptr._ZN4seal13parms_id_zeroE]+0x0): undefined reference to `seal::parms_id_zero'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\FHEProject.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr.__emutls_v._ZN4seal4util16global_variables15tls_memory_poolE[.refptr.__emutls_v._ZN4seal4util16global_variables15tls_memory_poolE]+0x0): undefined reference to `__emutls_v._ZN4seal4util16global_variables15tls_memory_poolE'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\FHEProject.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr._ZN4seal4util16global_variables18global_memory_poolE[.refptr._ZN4seal4util16global_variables18global_memory_poolE]+0x0): undefined reference to `seal::util::global_variables::global_memory_pool'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\FHEProject.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr._ZTVN4seal4util12MemoryPoolMTE[.refptr._ZTVN4seal4util12MemoryPoolMTE]+0x0): undefined reference to `vtable for seal::util::MemoryPoolMT'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [CMakeFiles\FHEProject.dir\build.make:100: FHEProject.exe] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/FHEProject.dir/all] Error 2
[build] mingw32-make: *** [Makefile:110: all] Error 2
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build d:/pj/build --config Debug --target all -j 10 -- exited with code: 2
[driver] Build completed: 00:00:02.129
[build] Build finished with exit code 2
解决方案

以下是针对该链接错误的排查和修复步骤:

1. 对齐编译器环境

从错误信息看,你使用MinGW(mingw64)编译器,必须确保SEAL库是用同一版本MinGW编译的,而非MSVC。如果SEAL用MSVC编译,MinGW链接时会因为符号规范不匹配出现undefined reference错误。

若之前用MSVC编译了SEAL,重新用MinGW编译:

  • 打开MinGW终端,进入SEAL源码目录
  • 执行:
    mkdir build && cd build
    cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
    mingw32-make -j4
    
    编译完成后,替换原有的lib文件。

2. 简化CMake配置,移除冗余设置

find_package(SEAL REQUIRED)会自动处理头文件包含,无需手动设置include_directories,修改后的CMakeLists.txt如下:

cmake_minimum_required(VERSION 3.10)
project(FHEProject VERSION 0.1.0 LANGUAGES C CXX)

# 指定SEAL配置文件路径
list(APPEND CMAKE_PREFIX_PATH D:/SEAL/build/cmake)

find_package(SEAL 4.1 REQUIRED)

add_executable(${PROJECT_NAME} main.cpp) 

# 链接SEAL库,添加PRIVATE确保属性正确传递
target_link_libraries(${PROJECT_NAME} PRIVATE SEAL::seal)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})

3. 验证CMake是否正确识别SEAL

CMake配置时,查看输出日志,确认find_package(SEAL)成功找到库文件,比如类似:

-- Found SEAL: D:/SEAL/build/lib/seal-4.1.lib (found version "4.1")

若未找到,检查CMAKE_PREFIX_PATH是否指向包含seal-config.cmake的目录(你的路径为D:/SEAL/build/cmake,确认该目录下存在此文件)。

4. 清理重建项目

  • 删除项目的build目录
  • 重新执行CMake配置:
    cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
    
  • 再次编译:
    mingw32-make -j4
    

5. 确认C++版本匹配

SEAL 4.x需要C++17支持,在项目CMakeLists.txt中添加以下代码,确保编译标准一致:

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 10:47:33