简体   繁体   English

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

[英]can't compile c program that uses openssl libraries

I am having a hard time finding this missing reference when running : gcc server.c -I /pwdmanlib/src -lssl -lcrypto -o server the include is my src files (headers needs etc..) and the rest is th required ssl libraries. 我很难在运行时找到此缺少的参考: gcc server.c -I /pwdmanlib/src -lssl -lcrypto -o server include是我的src文件(标头需要等。),其余是必需的ssl库。 I am getting the following output from gcc: 我从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

I found the OPENSSL_init_ssl function call in the ssl library and it is apparently getting included but can't be found by other references to it in the library?? 我在ssl库中发现了OPENSSL_init_ssl函数调用,显然已经包含了该函数,但在库中对其的其他引用找不到它? The includes from my program are specified below: 我的程序中的includes在下面指定:

ssl_funcs.h 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"

When linking in dynamic libraries with the -l option, these must occur last , after all other options: 当使用-l选项链接动态库时,它们必须在所有其他选项之后出现last

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

Besides this, you should address the warnings in your code. 除此之外,您应该在代码中解决警告。 These can potentially lead to undefined behavior . 这些可能导致不确定的行为

To add to dbush's answer above I also needed to compile with an explicit target directory for the linking library like so: gcc server.c -I/pwdmanlib/src -o server -L/usr/local/lib -lssl -lcrypto 要添加到上面的dbush答案中,我还需要使用链接库的显式目标目录进行编译,如下所示: gcc server.c -I/pwdmanlib/src -o server -L/usr/local/lib -lssl -lcrypto

Moreover, I also implemented a solution for this in CMake (build system I use for my projects) and provided that below as well in case anyway else might find that useful. 此外,我还在CMake(我用于项目的构建系统)中实现了此解决方案,并在下面提供了解决方案,以防其他情况可能有用。 This is just the pertinent portion of it, if anyone wants the full src to the cmake I would be more than happy to provide it. 这只是其中的相关部分,如果有人希望将完整的src发送给cmake,我将非常乐意提供。

CMakeLists.txt 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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM