如何使用OpenSSL编译一个简单的程序

How to compile a simple program with OpenSSL?

本文关键字:一个 简单 程序 何使用 OpenSSL 编译      更新时间:2023-10-16

我正在尝试编译一个简单的ssl程序(它取自openssl书的源代码)。该程序有以下文件:common.h common.c client.c server.c

我已经安装了openssl 0.9.7,所以我和这本书的版本相同。我已经下载了源代码和/在主目录中配置、制作、测试、安装。

在common.h中有以下内容:

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

我运行gcc-Wall common.c client.c-o客户端,但我得到以下错误:

common.c: In function ‘init_OpenSSL’:
common.c:12:5: warning: implicit declaration of function ‘THREAD_setup’
/tmp/ccvI3HX4.o: In function `handle_error':
common.c:(.text+0x3a): undefined reference to `ERR_print_errors_fp'
/tmp/ccvI3HX4.o: In function `init_OpenSSL':
common.c:(.text+0x51): undefined reference to `THREAD_setup'
common.c:(.text+0x5a): undefined reference to `SSL_library_init'
common.c:(.text+0x97): undefined reference to `SSL_load_error_strings'
/tmp/ccRA0Co9.o: In function `do_client_loop':
client.c:(.text+0x71): undefined reference to `BIO_write'
/tmp/ccRA0Co9.o: In function `main':
client.c:(.text+0xbb): undefined reference to `BIO_new_connect'
client.c:(.text+0x106): undefined reference to `BIO_ctrl'
client.c:(.text+0x18e): undefined reference to `BIO_free'
collect2: ld returned 1 exit status

显然,它无法链接到头文件。。。当我按照论坛中的建议运行时,我得到了

common.c: In function ‘init_OpenSSL’:
common.c:12:5: warning: implicit declaration of function ‘THREAD_setup’
/tmp/cc2gjx8W.o: In function `init_OpenSSL':
common.c:(.text+0x51): undefined reference to `THREAD_setup'
collect2: ld returned 1 exit status

但是添加-lpthread无助于解决问题。。。

知道为什么会发生这种情况以及如何解决吗?

我的猜测是,lcrypto和lssl默认安装在ubuntu中,这样做-lcypto就是告诉链接器查看系统头,而不是openssl安装头。。。

任何帮助或建议都将不胜感激!

谢谢!

在openssl书的一些代码版本中,与线程相关的函数存储在retrent.c中(事实上,在我看到的版本中,TRHEAD_setup的声明就在那里),所以尝试使用:

gcc -Wall common.c client.c reentrant.c -o client -lcrypto -lssl
相关文章: