0

I am coding an encryption program for educational purposes. I am using openssl as the encryption library. The problem is that I want to produce an executable that can run on systems without openssl installed. Therefor I need to link openssl statically. I am using a cmake_list for building.

As soon as I add the flag OPENSSL_USE_STATIC_LIBS TRUE I get linker errors... I have tried building openssl from sources and passing it to the find_package command but this still gives me a linker error for all openssl headers.

'''c++
cmake_minimum_required(VERSION 3.13)

project(encryption_test)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(OPENSSL_ROOT_DIR /usr/local/ssl)
set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)

include_directories(
    include/)
add_executable(${PROJECT_NAME}
    src/main.cpp)

target_link_libraries(${PROJECT_NAME}
    stdc++fs
    OpenSSL::SSL)
'''

The errors are like:

undefined reference to 'pthread_rwlock_init'

So many thanks for any help or ideas how I can fix this!

Simon Doll
  • 55
  • 5
  • 1
    When use 3d-party **static** libraries, it is your responsibility to link with all other libraries they need. This is because such infomation isn't contained in the *static* library, as opposite to a *dynamic* library. Undefined function is defined in the `pthread` library, so you need to link with that library. See [that question](https://stackoverflow.com/questions/1620918/cmake-and-libpthread) about using pthreads in CMake. – Tsyvarev Jul 04 '19 at 13:56
  • oh thanks i did not know that! – Simon Doll Jul 04 '19 at 14:13

1 Answers1

0

I actually solved the problem with an answer given in Static linking of OpenSSL Crypto in CMake

The hint target_link_libraries(program ${CMAKE_DL_LIBS}) solved my linker issues, just in case anyone else struggels with this.

Simon Doll
  • 55
  • 5