链接 OpenSSL 时不引用 BIO 函数

No reference to BIO-functions when linking OpenSSL

我正在尝试使用 openssl 引用编译一个 C 程序。我正在使用 Linux Mint 17.1 并安装了开发包 "libssl-dev"。

#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
...

void send_smtp_request(BIO *bio, const char *req)
{
    BIO_puts(bio, req);
    BIO_flush(bio);
    printf("%s", req);     
}

如果我编译代码:

gcc -o client bio-ssl-smtpcli2.c

我得到这个错误:

/tmp/ccCHrti2.o: In function 'send_smtp_request':
bio-ssl-smtpcli2.c:(.text+0x1f):  undefined reference to 'BIO_puts'
bio-ssl-smtpcli2.c:(.text+0x3a):  undefined reference to 'BIO_ctrl'

有人知道如何解决这个问题吗?

我设法通过使用来编译你的函数:

gcc main.c -o main -I /usr/local/ssl/include -L /usr/local/ssl/lib -lssl -lcrypto -Wall

更多解释:

  • -I /usr/local/ssl/include/usr/local/ssl/include 添加到包含搜索路径。

  • -L /usr/local/ssl/lib/usr/local/ssl/lib 添加到库搜索路径。

  • -lssl -lcrypto 链接库 libcrypto 和 libssl

  • -lcrypto 必须跟在 -lssl 之后,因为 ld 是单通道链接器

  • Wall 启用所有警告。

我猜你错过了 -lcrypto