Tyler Moore
Tyler Moore

Reputation: 43

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. I am getting the following output from 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?? The includes from my program are specified below:

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"

Upvotes: 1

Views: 5329

Answers (2)

Tyler Moore
Tyler Moore

Reputation: 43

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

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

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
)

Upvotes: 0

dbush
dbush

Reputation: 224917

When linking in dynamic libraries with the -l option, these must occur last, after all other options:

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.

Upvotes: 4

Related Questions