我在运行时很难找到这个丢失的引用:gcc server.c -I /pwdmanlib/src -lssl -lcrypto -o serverinclude是我的src文件(headers需要等…),其余是所需的ssl库。
我从gcc得到以下输出:

In file included from server.h:49:0,
                 from server.c:39:
/pwdmanlib/src/util/constants.h:30:0: warning: "LINE_MAX" redefined
 #define         LINE_MAX                2048
 ^
In file included from /usr/include/limits.h:147:0,
                 from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/limits.h:168,
                 from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/syslimits.h:7,
                 from /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed/limits.h:34,
                 from /pwdmanlib/src/util/constants.h:26,
                 from server.h:49,
                 from server.c:39:
/usr/include/x86_64-linux-gnu/bits/posix2_lim.h:81:0: note: this is the location of the previous definition
 #define LINE_MAX  _POSIX2_LINE_MAX
 ^
In file included from server.c:39:0:
server.h: In function ‘start_server’:
server.h:126:34: warning: comparison between pointer and integer
     if (p == NULL || listen_sock == NULL) {
                                  ^
In file included from server.c:39:0:
server.h: In function ‘routeClient’:
server.h:394:29: warning: passing argument 1 of ‘sendall’ makes pointer from integer without a cast [-Wint-conversion]
                 if (sendall(worker_sock, resp_data, fileLen) == -1) {
                             ^
In file included from server.c:39:0:
server.h:70:5: note: expected ‘SSL * {aka struct ssl_st *}’ but argument is of type ‘int’
 int sendall(SSL *ssl, char *buf, ssize_t *len);
     ^
/tmp/ccubinQD.o: In function `InitSSL':
server.c:(.text+0x1305): undefined reference to `OPENSSL_init_ssl'
server.c:(.text+0x1314): undefined reference to `OPENSSL_init_ssl'
server.c:(.text+0x1323): undefined reference to `OPENSSL_init_crypto'
/tmp/ccubinQD.o: In function `InitCTX':
server.c:(.text+0x1333): undefined reference to `TLS_server_method'
server.c:(.text+0x1350): undefined reference to `SSL_CTX_set_options'
collect2: error: ld returned 1 exit status

我在ssl库中找到了OPENSSL_in I t_ssl函数调用,它显然被包含进来了,但是在库中的其他引用找不到它??我的程序中包含的内容如下:
ssl函数h
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/crypto.h>

服务器.h
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>

#include "util/oop.h"
#include "util/stringops.h"
#include "util/constants.h"
#include "fawkes_proto.h"
#include "crypto/ssl_funcs.h"

服务器c
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>

#include "server.h"
#include "util/constants.h"

最佳答案

在使用-l选项链接动态库时,这些选项必须在所有其他选项之后最后出现:

gcc server.c -I /pwdmanlib/src -o server -lssl -lcrypto

除此之外,您还应该处理代码中的警告。这些可能导致undefined behavior

08-18 15:22