如何用OpenSSL编译一个简单的程序? [英] How to compile a simple program with OpenSSL?

查看:285
本文介绍了如何用OpenSSL编译一个简单的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已经安装了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 client 但是我收到以下错误:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' :5:warning:隐式声明函数'THREAD_setup'
/tmp/ccvI3HX4.o:函数`handle_error':
common.c :( .text + 0x3a):未定义的引用'ERR_print_errors_fp'
/tmp/ccvI3HX4.o在函数`init_OpenSSL'中:
common.c :( .text + 0x51):未定义的引用'THREAD_setup'
common.c :( .text + 0x5a):对`SSL_library_init'的未定义引用
common.c :(。text + 0x97):对'SSL_load_error_strings'的未定义引用
/tmp/ccRA0Co9.o:函数`do_client_loop':
client.c :(。text + 0x71):对'BIO_write'的未定义引用
/tmp/ccRA0Co9.o:函数`main':
client.c :( .text + 0xbb):未定义的引用'BIO_new_connect'
client.c :(。text + 0x106):未定义的引用'BIO_ctrl'
client.c :(。text + 0x18e) :未定义的引用'BIO_free'
collect2:ld返回1退出状态

显然它不能链接到头文件...
当我按照一个论坛中的建议运行时 gcc -Wall common.c client.c -o client -lcrypto -lssl 我得到

  common.c:函数'init_OpenSSL':
common.c:12:5:warning:隐式声明函数'THREAD_setup'
/tmp/cc2gjx8W.o在函数`init_OpenSSL'中:
common.c :(。text + 0x51):未定义引用'THREAD_setup'
collect2:ld返回1退出状态

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



任何想法为什么会发生这种情况以及如何解决它?我的猜测是,lcrypto和lssl默认安装在ubuntu中, lcypto告诉链接器查看系统头文件,而不是openssl安装文件...



任何帮助或指针都是赞赏

谢谢!

解决方案

在一些代码版本openssl book,与线程有关的函数存储在reentrant.c中(实际上是TRHEAD_setup的声明,在我看到的版本中),所以试试:

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


I am trying to compile a simple ssl program (it was taken from the openssl book source code). The program has the following files: common.h common.c client.c server.c

I have installed openssl 0.9.7 so I have the same version with the book. I have downloaded the source and ./Configure, make, make test, make install in the home directory.

In the common.h there are the following includes:

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

I run gcc -Wall common.c client.c -o client but I get the following errors:

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

Obviously it cannot link to the header files... When I run as suggested in one forum gcc -Wall common.c client.c -o client -lcrypto -lssl I get

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

But adding -lpthread won't help resolve the problem...

Any idea why this happens and how to solve it?

My guess is that lcrypto and lssl are installed by default in ubuntu and by doing -lcypto is telling the linker to look at the systems headers and not the openssl installation ones...

Any help or pointers is appreciated!

Thank you!

解决方案

In some code versions of openssl book, the thread related functions are stored in reentrant.c, (in fact the declaration of TRHEAD_setup, in the version i've seen is there), so try with:

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

这篇关于如何用OpenSSL编译一个简单的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆