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

编译wolfSSL代码报错“key存储大小未知”,已包含ecc.h求原因

解决编译报错:error: storage size of "key" is not known

核心原因

WolfSSL 默认将 ecc_key 定义为不透明类型,编译器无法获取其结构体的内存布局,直接声明变量就会触发"存储大小未知"的错误。此外代码里还有两处拼写错误,也会导致后续运行问题。

修复步骤

  • 添加预定义宏,让ecc_key结构体可见
    在包含任何WolfSSL头文件之前,定义WOLFSSL_ECC_SESSION宏,这样编译器能读取到ecc_key的完整结构体定义:

    #define WOLFSSL_ECC_SESSION
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <unistd.h> 
    #include <wolfssl/ssl.h> 
    #include <wolfssl/wolfcrypt/types.h> 
    #include <wolfssl/wolfcrypt/random.h> 
    #include <wolfssl/wolfcrypt/ecc.h> 
    #include <wolfssl/wolfcrypt/rsa.h> 
    
  • 修正代码拼写与格式问题

    1. 将mian改为main(主函数名拼写错误)
    2. 将arfv改为argv(命令行参数数组名拼写错误)
    3. 修正printf的换行符格式为printf("%d\n",ret);
    4. 补充资源释放代码,避免内存泄漏:调用wc_ecc_free(&key);和wc_FreeRng(&rng);
  • 确认WolfSSL已开启ECC支持
    若你自行编译WolfSSL,需确保编译时启用ECC选项:

    ./configure --enable-ecc
    make && make install
    

    若使用预编译包,确认安装版本包含ECC支持。

修复后的完整代码

#define WOLFSSL_ECC_SESSION
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <wolfssl/ssl.h> 
#include <wolfssl/wolfcrypt/types.h> 
#include <wolfssl/wolfcrypt/random.h> 
#include <wolfssl/wolfcrypt/ecc.h> 
#include <wolfssl/wolfcrypt/rsa.h> 

int main(int argc,char *argv[]){ 
    ecc_key key; 
    wc_ecc_init(&key); 
    WC_RNG rng; 
    wc_InitRng(&rng);
    int ret = 1; 
    ret = wc_ecc_make_key(&rng, 32, &key);
    printf("%d\n", ret); 

    // 释放资源
    wc_ecc_free(&key);
    wc_FreeRng(&rng);
    return 0;
} 

编译命令

保持原命令即可:

gcc file.c -o file -l wolfssl

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 21:10:38