【问题标题】:How to compile a simple program with OpenSSL?如何用 OpenSSL 编译一个简单的程序?
【发布时间】:2011-09-12 08:14:06
【问题描述】:

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

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

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 但出现以下错误:

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 client -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 无助于解决问题...

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

我的猜测是,在 ubuntu 中默认安装了 lcrypto 和 lssl,并且通过 -lcypto 告诉链接器查看系统头文件而不是 openssl 安装头文件...

感谢任何帮助或指点!

谢谢!

【问题讨论】:

  • 当你说headers时,你的意思是libraries吗?
  • 如果您担心链接到哪个库,请使用 -L 选项指定显式路径。

标签: c++ gcc openssl compilation ld


【解决方案1】:

在openssl book的一些代码版本中,线程相关的函数存放在reentrant.c中,(其实TRHEAD_setup的声明,在我看到的版本里就有),所以试试:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多