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

问题描述:

我想编译一个简单的ssl程序(它来自openssl书籍源代码)。 该程序有以下文件:common.h common.c client.c server.c如何使用OpenSSL编译一个简单的程序?

我已经安装了openssl 0.9.7,所以我与本书有相同的版本。 我已经下载了源码和./configure,make,make test,make install在home目录下。

在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 

显然它不能链接到头文件... 当我运行如在一个论坛建议gcc -Wall common.c client.c -o客户端-lcrypto -lssl我得到

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的安装者安装...

任何帮助或指针是不胜感激!

谢谢!

+0

当你说'标题'时,你的意思是'库'吗? – 2011-06-10 09:08:32

+0

是的抱歉,我的意思是库 – 2011-06-10 09:11:01

+0

如果您担心链接到哪个库,请使用-L选项指定显式路径。 – Nick 2011-06-10 10:08:44

OpenSSL中的书的一些代码版本,该线程相关功能都存储在reentrant.c,(其实TRHEAD_setup的声明,在我见过的版本是存在的),所以尝试使用:

gcc -Wall common.c client.c reentrant.c -o client -lcrypto -lssl 
+0

是的,你是对的!这解决了我的问题,虽然在我的版本reentrant.c甚至没有提及 – 2011-06-12 18:39:25

+0

我在哪里得到文件reentrant.c? – 2017-11-05 07:53:48