Static linking of OpenSSL Crypto in CMake

2

I need to make a cross-compiled OpenSSL for a MIPS device. I've tried following the documentation. Set OPENSSL_USE_STATIC_LIBS to true and set target_link_libraries to the library files you need.

CMakeLists.txt:

compileAsC99()

if(NOT ${use_http})
    message(FATAL_ERROR "program being generated without HTTP support")
endif()

set(program_c_files
    ...
)

set(program_h_files
    ...
)

include_directories(...)

add_executable(program ${program_c_files} ${program_h_files})

set(OPENSSL_USE_STATIC_LIBS TRUE)
#target_link_libraries(program OpenSSL::Crypto)
target_link_libraries(program /home/program/mips/lib/libssl.so.1.1)
target_link_libraries(program /home/program/mips/lib/libcrypto.so.1.1)

It compiles fine without warnings, but checking the resulting binary tells me that it's still shared library.

readelf -d program:

Dynamic section at offset 0x1bc contains 35 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libssl.so.1.1]
 0x00000001 (NEEDED)                     Shared library: [libcrypto.so.1.1]
 0x0000000f (RPATH)                      Library rpath: [/home/program/mips/lib]

I don't understand what I'm doing wrong.

EDIT: Already looked at Linking statically OpenSSL crypto library in CMake but it didn't tell me anything new.

EDIT 2: Updated the CMakeLists.txt file according to the reply: CMakeLists.txt:

compileAsC99()

if(NOT ${use_http})
    message(FATAL_ERROR "program being generated without HTTP support")
endif()

set(program_c_files
    ...
)

set(program_h_files
    ...
)

include_directories(...)

add_executable(program ${program_c_files} ${program_h_files})

find_package(OpenSSL REQUIRED)
if(OPENSSL_FOUND)
set(OPENSSL_USE_STATIC_LIBS TRUE)
message("OPENSSL FOUND!")
endif()
target_link_libraries(program OpenSSL::Crypto)

Output:

-- IoT Client SDK Version = 1.2.11
-- Provisioning client OFF
-- target architecture: GENERIC
-- Cross compiling not using pkg-config
-- Found CURL: /home/program/mips/lib/libcurl.a (found version "7.63.0")
-- Found CURL: /home/program/mips/lib/libcurl.a
-- target architecture: GENERIC
-- target architecture: GENERIC
-- target architecture: GENERIC
-- target architecture: GENERIC
-- iothub architecture: GENERIC
OPENSSL FOUND!
-- Configuring done
-- Generating done

EDIT PROSPERITY: If you, future people, run into the undefined reference to dlopen, I added the following to my CMakeLists.txt file

target_link_libraries(program ${CMAKE_DL_LIBS})
cmake
openssl
static-linking
asked on Stack Overflow Mar 14, 2019 by Kagemand Andersen • edited Mar 18, 2019 by Kagemand Andersen

1 Answer

5

Setting to TRUE, variable OPENSSL_USE_STATIC_LIBS forces find_package(OpenSSL) to search the static library. So this variable works only with that call, and if you use its results:

set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
target_link_libraries(program OpenSSL::Crypto)

If you have already executed cmake without setting of OPENSSL_USE_STATIC_LIBS, then you need to remove CMake cache (CMakeCache.txt under build directory) before new attempt. Otherwise, already found (shared!) libraries will be used and no re-search will be performed.

answered on Stack Overflow Mar 14, 2019 by Tsyvarev

User contributions licensed under CC BY-SA 3.0