用于OpenSSL库的体系结构i386的未定义符号

时间:2013-01-26 21:48:06

标签: c openssl

我正在通过Network Security with OpenSSL阅读本书Pravir Chandra, Matt Messier and John Viega,我正在尝试使用OpenSSL创建一个SSL客户端/服务器连接,但我收到此错误并且我不知道我知道为什么。

这是我的服务器:

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

#include <pthread.h>

#define PORT            "6001"
#define SERVER          "splat.zork.org"
#define CLIENT          "shell.zork.org"

void handle_error(const char *file, int lineno, const char *msg)
{
    fprintf(stderr, "** %s:%i %s\n", file, lineno, msg);
    ERR_print_errors_fp(stderr);
    exit(-1);
}

void init_OpenSSL(void)
{
    if (!THREAD_setup() || !SSL_library_init())
    {
        fprintf(stderr, "** OpenSSL initialization failed!\n");
        exit(-1);
    }
    SSL_load_error_strings();
}


void do_server_loop(BIO *conn)
{
    int  done, err, nread;
    char buf[80];

    do
    {
        for (nread = 0;  nread < sizeof(buf);  nread += err)
        {
             err = BIO_read(conn, buf + nread, sizeof(buf) - nread);
             if (err <= 0)
                 break;
        }
        fwrite(buf, 1, nread, stdout);
    }
    while (err > 0);
}

void *server_thread(void *arg)
{
    BIO *client = (BIO *)arg;

    pthread_detach(pthread_self(  ));

    fprintf(stderr, "Connection opened.\n");
    do_server_loop(client);
    fprintf(stderr, "Connection closed.\n");

    BIO_free(client);
    ERR_remove_state(0);
}

int main(int argc, char *argv[])
{
    BIO *acc, *client;

    pthread_t tid;

    init_OpenSSL();

    acc = BIO_new_accept(PORT);
    if (!acc)
        handle_error(__FILE__, __LINE__, "Error creating server socket");

    if (BIO_do_accept(acc) <= 0)
        handle_error(__FILE__, __LINE__, "Error binding server socket");

    for (;;)
    {
        if (BIO_do_accept(acc) <= 0)
            handle_error(__FILE__, __LINE__, "Error accepting connection");

        client = BIO_pop(acc);
        pthread_create(&tid, NULL, server_thread, client);
    }

    BIO_free(acc);
    return 0;
}

我正在使用gcc编译源:

$ gcc -o server server.c -lssl -crypto -lpthread -lm
Undefined symbols for architecture i386:
  "_ERR_print_errors_fp", referenced from:
      _handle_error in ccgAll3d.o
  "_THREAD_setup", referenced from:
      _init_OpenSSL in ccgAll3d.o
  "_BIO_read", referenced from:
      _do_server_loop in ccgAll3d.o
  "_BIO_free", referenced from:
      _server_thread in ccgAll3d.o
  "_ERR_remove_state", referenced from:
      _server_thread in ccgAll3d.o
  "_BIO_new_accept", referenced from:
      _main in ccgAll3d.o
  "_BIO_ctrl", referenced from:
      _main in ccgAll3d.o
  "_BIO_pop", referenced from:
      _main in ccgAll3d.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

有谁能告诉我为什么会这样?

前段时间我有类似的东西,通过定义有问题的功能解决了这个问题,但这已不再适用了。

我正在Mac OS X 10.6.8上编译源代码。 是因为我使用的是32位系统吗?

这是我正在使用gcc -Wall -lssl -crypto -lpthread -lm -o server server.c

编译的命令

2 个答案:

答案 0 :(得分:3)

您与gcc -o server sslserver.c -pthread -lssl稍微靠近一点。

同时the book's errata告知我们SSL_init_library应为SSL_library_init

唯一未定义的引用是'THREAD_setup'。

答案 1 :(得分:1)

您似乎没有与openssl库链接。尝试添加

[..] -lssl [..]

除链接之外的其他库。

您的gcc-line似乎也不完整:

 gcc -o -lpthread -lm server.c 

-o参数后面应该是output-filename(在这种情况下,它将使用“-lpthread”作为文件名。