2017-03-16 213 views
0

我在运行时很难找到这个缺失的引用:gcc server.c -I /pwdmanlib/src -lssl -lcrypto -o server include是我的src文件(头文件需要等等),其余的是所需的ssl库。 我得到以下输出从GCC:无法编译使用openssl库的c程序

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" 
+0

'server.h:70:5:注:应为 'SSL * {又名结构ssl_st *}',但参数的类型为“int''是不是找一个有点好。你应该用你的代码来解决这些问题。编写良好的代码应该编译时根本没有警告。 – Havenard

回答

2

当与动态链接库-l选项,这些必须发生last,毕竟其他选项:

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

除此之外,您应该解决您的代码的警告。这些可能会导致undefined behavior

+0

感谢您的快速回复,我移动了-o选项,仍然收到编译错误。 cmd看起来像这样:'gcc -I/pwdmanlib/src server.c -o server -L/usr/local/ssl/include/-L/usr/local/ssl/lib -lssl -lcrypto'甚至复制并粘贴你的代码也是一样的.. –

0

为了增加dbush的回答上面我也需要有一个明确的目标目录来编译,像这样的链接库:gcc server.c -I/pwdmanlib/src -o server -L/usr/local/lib -lssl -lcrypto

此外,我还实施了CMake的一个解决方案(编译系统我用我的项目),并提供以下情况,否则可能会发现有用。这只是它的相关部分,如果任何人想要cmake的完整src,我会非常乐意提供它。

的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 
)