我有一个 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/