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

已链接libssl与libcrypto仍遇OpenSSL符号未定义错误求解决

解决OpenSSL函数未定义引用错误

问题场景

编写文本转SHA3-256哈希的函数时,使用OpenSSL库出现undefined reference链接错误,相关代码及编译输出如下:

示例代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/sha.h>

void sha3_256_hash(const char *input, unsigned char *output) {
    EVP_MD_CTX *mdctx;
    const EVP_MD *md;
    unsigned int md_len;

    md = EVP_sha3_256();

    mdctx = EVP_MD_CTX_new();

    EVP_DigestInit_ex(mdctx, md, NULL);

    EVP_DigestUpdate(mdctx, input, strlen(input));

    EVP_DigestFinal_ex(mdctx, output, &md_len);

    EVP_MD_CTX_free(mdctx);
}

int main() {
    const char *password = "Hello_World";
    unsigned char hash[SHA256_DIGEST_LENGTH];

    sha3_256_hash(password, hash);

    printf("SHA-3-256 Hash: ");
    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
        printf("%02x", hash[i]);
    }
    printf("\n");

    return 0;
}

编译错误输出

**** Incremental Build of configuration Debug for project Test ****
Info: Internal Builder is used for build
gcc "-IC:\vcpkg\packages\openssl_x64-windows\include" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\Test.o" "..\src\Test.c" 
gcc "-LC:\vcpkg\packages\openssl_x64-windows\lib" -o Test.exe "src\Test.o" -llibssl -llibcrypto 
src\Test.o: In function `sha3_256_hash':
C:\Users\null_object\Documents\c_workspace\Test\Debug/../src/Test.c:12: undefined reference to `EVP_sha3_256'
C:\Users\null_object\Documents\c_workspace\Test\Debug/../src/Test.c:14: undefined reference to `EVP_MD_CTX_new'
C:\Users\null_object\Documents\c_workspace\Test\Debug/../src/Test.c:16: undefined reference to `EVP_DigestInit_ex'
C:\Users\null_object\Documents\c_workspace\Test\Debug/../src/Test.c:18: undefined reference to `EVP_DigestUpdate'
C:\Users\null_object\Documents\c_workspace\Test\Debug/../src/Test.c:20: undefined reference to `EVP_DigestFinal_ex'
C:\Users\null_object\Documents\c_workspace\Test\Debug/../src/Test.c:22: undefined reference to `EVP_MD_CTX_free'
collect2.exe: error: ld returned 1 exit status

Build Failed. 6 errors, 0 warnings. (took 446ms)

修复方案

这类错误是链接阶段找不到OpenSSL库中的对应符号导致的,可通过以下步骤解决:

  • 调整库的链接顺序:GCC链接器对库的顺序有要求,依赖的库要放在被依赖的库之后。将编译命令中的库移到目标文件后,且libcrypto(包含哈希核心函数)放在libssl前面:

    gcc "-IC:\vcpkg\packages\openssl_x64-windows\include" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\Test.o" "..\src\Test.c"
    gcc "-LC:\vcpkg\packages\openssl_x64-windows\lib" -o Test.exe "src\Test.o" -llibcrypto -llibssl
    
  • 确认OpenSSL版本:EVP_sha3_256()是OpenSSL 1.1.0及以上版本才支持的函数,检查vcpkg安装的OpenSSL版本是否符合要求,若版本过低需升级。

  • 验证库文件路径:确认-LC:\vcpkg\packages\openssl_x64-windows\lib路径下存在对应模式(Debug/Release)的库文件,比如Debug模式下的libcryptoMDd.lib、libsslMDd.lib,Release模式下的libcryptoMD.lib、libsslMD.lib。

  • 代码严谨性修正:代码中用SHA256_DIGEST_LENGTH定义哈希数组长度虽然可行,但更严谨的方式是通过OpenSSL函数获取对应哈希算法的长度:

    // 在sha3_256_hash函数中获取哈希长度
    unsigned int md_len = EVP_MD_size(md);
    // main函数中可改为通用长度定义
    unsigned char hash[EVP_MAX_MD_SIZE];
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 23:50:10