【问题标题】:Undefined symbols for architecture i386 for OpenSSL libraryOpenSSL 库的体系结构 i386 的未定义符号
【发布时间】:2013-01-26 21:48:05
【问题描述】:

我正在阅读Pravir Chandra, Matt Messier and John Viega 撰写的这本书Network Security with OpenSSL,并且我正在尝试在书中使用 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编译的命令

【问题讨论】:

    标签: c openssl


    【解决方案1】:

    gcc -o server sslserver.c -pthread -lssl 会让你更亲近。

    另外the book's errata 告诉我们SSL_init_library 应该是SSL_library_init。

    剩下的唯一未定义引用是“THREAD_setup”。

    【讨论】:

    • 不要移动选项。图书馆归根结底是有原因的。试试gcc -o server server.c -Wall -lssl -crypto -lpthread -lm(我几乎可以肯定它应该是-pthread 而不是-lpthread,但是-lpthread 本身并没有什么坏处,而且在你的操作系统上它甚至可能是正确的)。
    • 与“相似”中的相同(应该是预期的),还是相同的“逐字”?不管怎样,用新的命令行和新的输出更新你的问题。
    • 更新了命令输出和源代码以纠正SSL_init_library问题。
    • -crypto 而不是 -lcrypto 是我的错字,抱歉。 -lcrypto 是您所需要的。
    • 是的。我会尝试删除THREAD_setup:这似乎是线程支持的一些系统范围的初始化,现代系统不应该需要它。
    【解决方案2】:

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

    [..] -lssl [..]
    

    链接时除了您的其他库。

    您的 gcc 行似乎也不完整:

     gcc -o -lpthread -lm server.c 
    

    -o 参数后面应该跟一个输出文件名(在这种情况下,它将使用“-lpthread”作为文件名。

    【讨论】:

    • 我编辑了帖子以指定我用于编译的命令,并且我正在使用 -lssl 选项。
    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多