c - 在 openssl/sha.h 中声明但在共享库中找不到的函数

标签 c shared-libraries ld libssl

我尝试将我的 C 程序链接到 libssl.so,但链接器无法在该库中找到头文件中确实存在的函数。

Makefile目标代码:

$(CC) -o $@ $^ $(CFLAGS) -lssl -lmagic

输出:

...
/usr/bin/ld: obj/signature.o: undefined reference to symbol 'SHA256_Init@@OPENSSL_1_1_0'
//usr/lib/x86_64-linux-gnu/libcrypto.so.1.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
...

该函数是 SHA256_Init(),它存在于源文件中包含的 openssl/sha.h 中。

我使用的是 Debian 9。我安装了以下软件包:libssl1.0.0libssl1.0.2libssl1.1libssl-dev (1.1.0)。

$ la /usr/lib/x86_64-linux-gnu/libssl*
-rw-r--r-- root root 357024 /usr/lib/x86_64-linux-gnu/libssl3.so
-rw-r--r-- root root 738444 /usr/lib/x86_64-linux-gnu/libssl.a
lrwxrwxrwx root root     13 /usr/lib/x86_64-linux-gnu/libssl.so -> libssl.so.1.1
-rw-r--r-- root root 395176 /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0
-rw-r--r-- root root 431232 /usr/lib/x86_64-linux-gnu/libssl.so.1.0.2
-rw-r--r-- root root 442920 /usr/lib/x86_64-linux-gnu/libssl.so.1.1

我做错了什么以及如何解决这个问题?

最佳答案

OpenSSL 分为 2 个库,libssl 和 libcrypto,这些函数位于 libcrypto 中,因此您不需要链接到 libssl。使用

 -lcrypto

您还应该阅读此documentation欲了解更多信息,例如如何正确初始化libcrypto

关于c - 在 openssl/sha.h 中声明但在共享库中找不到的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45889307/

相关文章:

c++ - 定义集合 C++

android - cc_prebuilt_library_shared 模块无法识别的属性 "cflags"

c++11 - 克里昂和 CMake。如何添加库 (*.so)

c - 与另一个启动文件链接

ld - 如何使用 GNU ld 创建一个空部分?

c - 在 C 中使用 strstr

c - 请解释这段代码在做什么(someChar - 48)

c++ - char数组的pid_t的初始值是什么?

c++ - 通过 .def 文件在 Linux 上 Hook 函数

c - 获取可执行文件中文本部分的开始和结束地址