无法编译使用openssl库的c程序

时间:2017-03-16 19:32:54

标签: c ssl gcc

我在运行时很难找到这个缺失的引用:gcc server.c -I /pwdmanlib/src -lssl -lcrypto -o server include是我的src文件(头文件需要等等),其余的是必需的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_init_ssl函数调用,它显然已被包含但在库中对其进行的其他引用无法找到?我的计划中包含的内容如下:

ssl_funcs.h

#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/crypto.h>

server.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"

server.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"

2 个答案:

答案 0 :(得分:2)

在动态库中使用-l选项进行关联时,必须在所有其他选项之后出现最后

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

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

答案 1 :(得分:0)

要添加到dbush的上面的答案,我还需要使用链接库的显式目标目录进行编译,如下所示:gcc server.c -I/pwdmanlib/src -o server -L/usr/local/lib -lssl -lcrypto

此外,我还在CMake(我用于我的项目的构建系统)中为此实现了一个解决方案,并提供了以下内容,以防万一其他方面可能会发现它很有用。这只是它的相关部分,如果有人想要完整的src到cmake,我会非常乐意提供它。

的CMakeLists.txt

# Add libraries
include_directories(${LOCAL_LIBS_DIR})
include_directories("/usr/local/lib")
#link_directories("/usr/local/lib")

add_library(ssl SHARED IMPORTED) # or STATIC instead of SHARED
set_property(TARGET ssl PROPERTY IMPORTED_LOCATION "/usr/local/lib/libssl.so")
add_library(crypto SHARED IMPORTED) # or STATIC instead of SHARED
set_property(TARGET crypto PROPERTY IMPORTED_LOCATION "/usr/local/lib/libcrypto.so")
#include_directories("/opt/openssl-1.1.0e")

#find_package (my_library COMPONENTS REQUIRED component1 component2  OPTIONAL_COMPONENTS opt_component)

# Define build targets and link libraries
add_executable(main ${SOURCE_FILES})
target_include_directories(main PUBLIC /usr/include/openssl)
target_link_libraries(main
        PRIVATE ${Boost_LIBRARIES}
        PRIVATE ${PostgreSQL_LIBRARIES}
        PRIVATE ${cJSON_ROOT_DIR}
#       PRIVATE ${CryptoPP_ROOT_DIR}
#       PRIVATE ${Kore_ROOT_DIR}
#       PRIVATE ${POCO_LIBRARIES}
        PRIVATE ssl
        PRIVATE crypto
)