C: 动态链接 OpenSSL 库时出错

标签 c linux gcc linker openssl

我有一个 C 程序,它使用 OpenSSL 库中的 AES 例程。 我安装了必要的库 (libssl-dev)

我可以静态编译我的程序:

gcc -s -o aes aes.c /usr/lib/x86_64-linux-gnu/libcrypto.a

但是当我尝试动态编译它时,出现以下错误:

$ gcc -s -o aes aes.c -lcrypto
/tmp/ccofFr4N.o: In function `encrypt':
aes.c:(.text+0x9f): undefined reference to `aesni_set_encrypt_key'
aes.c:(.text+0xd9): undefined reference to `aesni_cbc_encrypt'
aes.c:(.text+0x1a0): undefined reference to `aesni_cbc_encrypt'
/tmp/ccofFr4N.o: In function `decrypt':
aes.c:(.text+0x2d4): undefined reference to `aesni_set_decrypt_key'
aes.c:(.text+0x31e): undefined reference to `aesni_cbc_encrypt'
collect2: error: ld returned 1 exit status

为什么我不能使用动态链接的 OpenSSL 编译我的程序?

更新

因此,这是来自 libssl-dev 的库:

$ ls /usr/lib/x86_64-linux-gnu/libcrypto.*
/usr/lib/x86_64-linux-gnu/libcrypto.a
/usr/lib/x86_64-linux-gnu/libcrypto.so
/usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0

我注意到以下几点。当我用 grep 查找 AES 函数的名称时,只有 libcrypto.a 匹配。

$ grep aesni_set_encrypt_key /usr/lib/x86_64-linux-gnu/libcrypto.*
Binary file /usr/lib/x86_64-linux-gnu/libcrypto.a matches

/usr/lib/x86_64-linux-gnu/libcrypto.so 不应该也包含这些函数吗?

最佳答案

很可能 /usr/lib/x86_64-linux-gnu/ 不是编译器搜索路径的一部分。

建议,在-lcrypto前插入参数:-L/usr/lib/x86_64-linux-gnu/

关于C: 动态链接 OpenSSL 库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35130738/

相关文章:

c++ - GCC 中的 std::put_time 实现状态?

c - DES 的三重 DES 实现

c - uchar 的模拟内存数组中的 int

linux - 如何在 bash 中终止运行时间最长的进程实例

linux - 如何在 Linux 中用 awk 比较连续的区分大小写的单词?

c - "Variable to be used not initialized"警告

c++ - Void 类型函数不打印出结构成员变量

c - 从recvfrom获取缓冲区

c - 为什么 sizeof unsigned char array[10] 是 10

linux - 如何使用 XCB 或 X11 显示全彩色图像?