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

编译Dilithium2算法时宏定义函数出现未定义引用错误求助

Dilithium2量子密码算法编译错误:宏定义函数未定义引用问题排查与解决

我在编译Dilithium2量子密码学项目生成可执行文件时,遇到宏定义函数相关的编译问题:已通过宏定义声明的函数,在链接阶段仍出现大量未定义引用错误。以下是详细信息及解决方案。


编译输出及错误信息

====================[ Build | dilithium_test_speed | Debug ]====================
/opt/clion-2023.1.4/bin/cmake/linux/x64/bin/cmake --build /projectDirectory/cmake-build-debug --target dilithium_test_speed -j 10
[2/2] Linking C executable dilithium_test_speed
FAILED: dilithium_test_speed 
: && /usr/bin/cc -g  CMakeFiles/dilithium_test_speed.dir/test/cpucycles.c.o CMakeFiles/dilithium_test_speed.dir/test/speed_print.c.o CMakeFiles/dilithium_test_speed.dir/test/test_dilithium.c.o CMakeFiles/dilithium_test_speed.dir/randombytes.c.o CMakeFiles/dilithium_test_speed.dir/sign.c.o CMakeFiles/dilithium_test_speed.dir/poly.c.o CMakeFiles/dilithium_test_speed.dir/polyvec.c.o -o dilithium_test_speed  -lcrypto  -lssl && :
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/sign.c.o: in function `pqcrystals_dilithium2_ref_keypair':
/projectDirectory/sign.c:33: undefined reference to `pqcrystals_fips202_ref_shake256'
/usr/bin/ld: /projectDirectory/sign.c:58: undefined reference to `pqcrystals_dilithium2_ref_pack_pk'
/usr/bin/ld: /projectDirectory/sign.c:61: undefined reference to `pqcrystals_fips202_ref_shake256'
/usr/bin/ld: /projectDirectory/sign.c:62: undefined reference to `pqcrystals_dilithium2_ref_pack_sk'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/sign.c.o: in function `pqcrystals_dilithium2_ref_signature':
/projectDirectory/sign.c:100: undefined reference to `pqcrystals_dilithium2_ref_upack_sk'
/usr/bin/ld: /projectDirectory/sign.c:103: undefined reference to `pqcrystals_fips202_ref_shake256_init'
/usr/bin/ld: /projectDirectory/sign.c:104: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/sign.c:105: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/sign.c:106: undefined reference to `pqcrystals_fips202_ref_shake256_finalize'
/usr/bin/ld: /projectDirectory/sign.c:107: undefined reference to `pqcrystals_fips202_ref_shake256_squeeze'
/usr/bin/ld: /projectDirectory/sign.c:112: undefined reference to `pqcrystals_fips202_ref_shake256'
/usr/bin/ld: /projectDirectory/sign.c:137: undefined reference to `pqcrystals_fips202_ref_shake256_init'
/usr/bin/ld: /projectDirectory/sign.c:138: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/sign.c:139: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/sign.c:140: undefined reference to `pqcrystals_fips202_ref_shake256_finalize'
/usr/bin/ld: /projectDirectory/sign.c:141: undefined reference to `pqcrystals_fips202_ref_shake256_squeeze'
/usr/bin/ld: /projectDirectory/sign.c:176: undefined reference to `pqcrystals_dilithium2_ref_pack_sig'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/sign.c.o: in function `pqcrystals_dilithium2_ref_verify':
/projectDirectory/sign.c:245: undefined reference to `pqcrystals_dilithium2_ref_unpack_pk'
/usr/bin/ld: /projectDirectory/sign.c:246: undefined reference to `pqcrystals_dilithium2_ref_unpack_sig'
/usr/bin/ld: /projectDirectory/sign.c:252: undefined reference to `pqcrystals_fips202_ref_shake256'
/usr/bin/ld: /projectDirectory/sign.c:253: undefined reference to `pqcrystals_fips202_ref_shake256_init'
/usr/bin/ld: /projectDirectory/sign.c:254: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/sign.c:255: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/sign.c:256: undefined reference to `pqcrystals_fips202_ref_shake256_finalize'
/usr/bin/ld: /projectDirectory/sign.c:257: undefined reference to `pqcrystals_fips202_ref_shake256_squeeze'
/usr/bin/ld: /projectDirectory/sign.c:281: undefined reference to `pqcrystals_fips202_ref_shake256_init'
/usr/bin/ld: /projectDirectory/sign.c:282: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/sign.c:283: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/sign.c:284: undefined reference to `pqcrystals_fips202_ref_shake256_finalize'
/usr/bin/ld: /projectDirectory/sign.c:285: undefined reference to `pqcrystals_fips202_ref_shake256_squeeze'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_reduce':
/projectDirectory/poly.c:33: undefined reference to `pqcrystals_dilithium2_ref_reduce32'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_caddq':
/projectDirectory/poly.c:51: undefined reference to `pqcrystals_dilithium2_ref_caddq'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_freeze':
/projectDirectory/poly.c:69: undefined reference to `pqcrystals_dilithium2_ref_freeze'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_ntt':
/projectDirectory/poly.c:143: undefined reference to `pqcrystals_dilithium2_ref_ntt'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_invntt_tomont':
/projectDirectory/poly.c:160: undefined reference to `pqcrystals_dilithium2_ref_invntt_tomont'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_pointwise_montgomery':
/projectDirectory/poly.c:181: undefined reference to `pqcrystals_dilithium2_ref_montgomery_reduce'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_power2round':
/projectDirectory/poly.c:203: undefined reference to `pqcrystals_dilithium2_ref_power2round'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_decompose':
/projectDirectory/poly.c:226: undefined reference to `pqcrystals_dilithium2_ref_decompose'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_make_hint':
/projectDirectory/poly.c:249: undefined reference to `pqcrystals_dilithium2_ref_make_hint'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_use_hint':
/projectDirectory/poly.c:271: undefined reference to `pqcrystals_dilithium2_ref_use_hint'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_uniform':
/projectDirectory/poly.c:372: undefined reference to `pqcrystals_dilithium2_ref_dilithium_shake128_stream_init'
/usr/bin/ld: /projectDirectory/poly.c:373: undefined reference to `pqcrystals_fips202_ref_shake128_squeezeblocks'
/usr/bin/ld: /projectDirectory/poly.c:382: undefined reference to `pqcrystals_fips202_ref_shake128_squeezeblocks'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_uniform_eta':
/projectDirectory/poly.c:462: undefined reference to `pqcrystals_dilithium2_ref_dilithium_shake128_stream_init'
/usr/bin/ld: /projectDirectory/poly.c:463: undefined reference to `pqcrystals_fips202_ref_shake128_squeezeblocks'
/usr/bin/ld: /projectDirectory/poly.c:468: undefined reference to `pqcrystals_fips202_ref_shake128_squeezeblocks'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_uniform_gamma1':
/projectDirectory/poly.c:496: undefined reference to `pqcrystals_dilithium2_ref_dilithium_shake256_stream_init'
/usr/bin/ld: /projectDirectory/poly.c:497: undefined reference to `pqcrystals_fips202_ref_shake256_squeezeblocks'
/usr/bin/ld: CMakeFiles/dilithium_test_speed.dir/poly.c.o: in function `pqcrystals_dilithium2_ref_poly_challenge':
/projectDirectory/poly.c:517: undefined reference to `pqcrystals_fips202_ref_shake256_init'
/usr/bin/ld: /projectDirectory/poly.c:518: undefined reference to `pqcrystals_fips202_ref_shake256_absorb'
/usr/bin/ld: /projectDirectory/poly.c:519: undefined reference to `pqcrystals_fips202_ref_shake256_finalize'
/usr/bin/ld: /projectDirectory/poly.c:520: undefined reference to `pqcrystals_fips202_ref_shake256_squeezeblocks'
/usr/bin/ld: /projectDirectory/poly.c:532: undefined reference to `pqcrystals_fips202_ref_shake256_squeezeblocks'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

项目文件结构

├── test
│     ├── cpucycles.c
│     ├── cpucycles.h
│     ├── speed_print.c
│     ├── speed_print.h
│     ├── test_dilithium.c
│     ├── test_mul.c
│     ├── test_speed.c
│     └── test_vectors.c
├── aes256ctr.c
├── aes256ctr.h
├── api.h
├── config.h
├── fips202.c
├── fips202.h
├── ntt.c
├── ntt.h
├── packing.c
├── packing.h
├── params.h
├── poly.c
├── poly.h
├── polyvec.c
├── PQCgenKAT_sign.c
├── randombytes.c
├── randombytes.h
├── reduce.c
├── reduce.h
├── rng.c
├── rng.h
├── rounding.c
├── rounding.h
├── sign.c
├── sign.h
├── symmetric.h
├── symmetric-aes.c
├── symmetric-shake.c
├── Makefile
├── CMakelists.txt

当前CMakeLists.txt配置

cmake_minimum_required(VERSION 3.12)
project(dilithium2 C)

set(CMAKE_C_STANDARD 11)

# Add your source files here
set(SOURCES
    aes256ctr.c
    fips202.c
    ntt.c
    packing.c
    poly.c
    polyvec.c
    PQCgenKAT_sign.c
    randombytes.c
    reduce.c
    rng.c
    rounding.c
    sign.c
    symmetric-aes.c
    symmetric-shake.c
)

# Add your test source files here
set(TEST_SOURCES
    test/cpucycles.c
    test/speed_print.c
    test/test_dilithium.c
    sign.c
    poly.c
    polyvec.c
)

# Add include directories
include_directories(
    .
    test
)

add_executable(dilithium_test_speed ${TEST_SOURCES})

# Link any necessary libraries (e.g., -lcrypto -lssl)
target_link_libraries(dilithium_test_speed crypto ssl)

问题分析与解决方案

1. 宏定义函数的正确使用技巧

  • 确保宏定义作用域覆盖所有引用文件:宏定义需在公共头文件(如params.h或config.h)中声明,所有引用宏函数的源文件必须包含该头文件,保证编译时宏能正确展开。
  • 验证宏展开结果:Dilithium中函数名通过宏动态生成(如pqcrystals_dilithium2_ref_*),需确认编译时已正确定义DILITHIUM2宏,可通过编译参数-DDILITHIUM2强制指定,或在config.h中启用对应参数集。
  • 避免宏冲突:检查是否存在重复定义的宏,或未定义必要的配置宏(如PARAMS_DILITHIUM2),导致函数名展开异常。

2. 针对当前编译错误的具体修复

当前错误的核心原因是测试可执行文件未链接所有依赖的核心源文件:TEST_SOURCES仅包含了部分源文件,缺少fips202.c、reduce.c、ntt.c、packing.c等实现文件,这些文件包含了错误中提到的未定义函数的具体实现。

推荐采用「核心库+测试可执行文件」的编译方式,避免重复添加源文件:

修正后的CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(dilithium2 C)

set(CMAKE_C_STANDARD 11)

# 核心库源文件:包含所有Dilithium2的实现代码
set(CORE_SOURCES
    aes256ctr.c
    fips202.c
    ntt.c
    packing.c
    poly.c
    polyvec.c
    randombytes.c
    reduce.c
    rng.c
    rounding.c
    sign.c
    symmetric-aes.c
    symmetric-shake.c
)

# 测试源文件:仅包含测试相关代码
set(TEST_SOURCES
    test/cpucycles.c
    test/speed_print.c
    test/test_dilithium.c
)

# 添加头文件目录
include_directories(
    .
    test
)

# 编译核心静态库,确保所有实现代码被编译
add_library(dilithium2_core STATIC ${CORE_SOURCES})
# 强制指定Dilithium2参数集的宏定义
target_compile_definitions(dilithium2_core PUBLIC DILITHIUM2)

# 编译测试可执行文件,链接核心库及第三方依赖
add_executable(dilithium_test_speed ${TEST_SOURCES})
target_link_libraries(dilithium_test_speed dilithium2_core crypto ssl)

修复说明

  • 通过add_library将核心代码编译为静态库,确保所有依赖函数的实现都被编译进库文件。
  • 使用target_compile_definitions明确指定DILITHIUM2宏,保证函数名正确展开为pqcrystals_dilithium2_ref_*系列。
相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 23:53:04