16

I want to statically link the libraries listed below:

set_target_properties(exec PROPERTIES LINK_SEARCH_START_STATIC 1)
set_target_properties(exec PROPERTIES LINK_SEARCH_END_STATIC 1)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})

find_library(SODIUM_LIB libsodium.a REQUIRED)
find_library(SSL_LIB libssl.a REQUIRED)
find_library(CRYPTO_LIB libcrypto.a REQUIRED)
find_library(DL_LIB libdl.a REQUIRED)

message(${SODIUM_LIB})
message(${SSL_LIB})
message(${CRYPTO_LIB})

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")

target_link_libraries(
    exec
    ${SODIUM_LIB}
    ${SSL_LIB}
    ${CRYPTO_LIB}
    ${DL_LIB}

I do not want to add -static to CMAKE_EXE_LINKER_FLAGS, because in that case everything is linked static. CMake finds the static libraries:

/usr/local/lib/libsodium.a
/usr/lib/x86_64-linux-gnu/libssl.a
/usr/lib/x86_64-linux-gnu/libcrypto.a

and it seems that everything is linked statically except libcrypto:

readelf -d exec
 0x0000000000000001 (NEEDED)             Shared library: [libcrypto.so.1.0.0]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]

How can I link libcrypto statically to my executable?

1
  • have you checked you actually find the right library? Also it may help to optionally define the path where the static library is to be found.
    – Ælex
    Jul 23, 2016 at 19:00

1 Answer 1

23

The CMake documentation starting with version 3.4 on the FindOpenSSL page says:

Set OPENSSL_USE_STATIC_LIBS to TRUE to look for static libraries.

(Assuming if they are found they will be used)

Example:

cmake_minimum_required(VERSION 3.4)
project(Foo)

set(SOURCE_FILES main.cpp)

set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} OpenSSL::Crypto)
5
  • As stated by @Tsyvarev in the comments, OPENSSL_USE_STATIC_LIBS is an hint in finding libraries and should prepend find_package(OpenSSL). Verified in FindOpenSSL.cmake source, edited accordingly.
    – ceztko
    Apr 23, 2019 at 9:26
  • "should prepend" -> "should come before". Prepend is the opposite of append. In any case, things don't prepend things, let alone themselved. Things are prepended to other things.
    – rubenvb
    Apr 23, 2019 at 9:28
  • @rubenvb Both of the examples on dictionary.cambridge.org/dictionary/english/prepend use prepend in the active voice.
    – Omegastick
    Jan 22, 2020 at 5:52
  • @Omegastick Yes, active voice when A is prepending B to C, not when D "is prepending" itself to E.
    – rubenvb
    Jan 22, 2020 at 10:17
  • 1
    bickering aside, "precede" seems more clear to me. "prepend" almost implies concatenation Jul 21, 2021 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.