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

如何编译引入<openssl/sha.h>的C程序?SHA-256登录系统编译报错

解决OpenSSL头文件找不到的编译问题

Windows 平台

使用 MinGW 编译

假设你的 OpenSSL 安装在 C:\OpenSSL-win64,编译时需手动指定头文件路径和链接库,执行以下命令:

gcc your_code.c -o your_program -I"C:\OpenSSL-win64\include" -L"C:\OpenSSL-win64\lib" -lcrypto
  • -I:指定 OpenSSL 头文件所在目录
  • -L:指定 OpenSSL 库文件所在目录
  • -lcrypto:链接 OpenSSL 的加密库

使用 MSVC 编译

在 VS Code 的 tasks.json 中配置编译参数,添加头文件包含目录和链接库路径:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "type": "shell",
      "command": "cl.exe",
      "args": [
        "/Zi",
        "/EHsc",
        "/I", "C:\\OpenSSL-win64\\include",
        "your_code.c",
        "/link",
        "/LIBPATH:C:\\OpenSSL-win64\\lib",
        "libcrypto.lib",
        "/OUT:your_program.exe"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

保存后执行编译任务即可。运行前需将 OpenSSL 目录下对应版本的动态库(如 libcrypto-1_1-x64.dll)放到程序目录或系统 PATH 中。

Linux/macOS 平台

包管理器安装的 OpenSSL

如果是通过系统包管理器安装的 OpenSSL 开发包(如 Ubuntu 的 libssl-dev,macOS Homebrew 的 openssl),直接用以下命令编译:

# Linux
gcc your_code.c -o your_program -lcrypto

# macOS
gcc your_code.c -o your_program -I$(brew --prefix openssl)/include -L$(brew --prefix openssl)/lib -lcrypto

手动编译安装的 OpenSSL

若 OpenSSL 手动安装在 /usr/local/ssl,编译时指定路径并添加运行时库路径:

gcc your_code.c -o your_program -I/usr/local/ssl/include -L/usr/local/ssl/lib -lcrypto -Wl,-rpath=/usr/local/ssl/lib

-Wl,-rpath 用于让程序运行时能找到 OpenSSL 库文件。

注意事项

  • 确认 OpenSSL 安装路径下的 include/openssl 目录中存在 sha.h 文件
  • Windows 平台需保证编译器(32/64位)与 OpenSSL 版本匹配
  • 运行程序前确保系统能找到 OpenSSL 的动态库文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 03:07:22