且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Openssl如何编译一个简单的程序?

更新时间:2023-09-30 13:31:16

在openssl书的一些代码版本中,线程相关函数存储在reentrant.c中,的TRHEAD_setup,在我看到的版本中有),所以尝试:

  gcc -Wall common.c客户端。 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