mercy
mercy

Reputation: 11

How to build and link OpenSSL and libCURL statically using CMake with FetchContent?

I am trying to use CMake build system using the CLion IDE for a project. I need to get libCURL and OpenSSL from the github repo using FetchContent. I want to first build OpenSSL statically and then tell CMake to pass the OpenSSL::SSL to libCURL so it can statically link it. Also note that since OpenSSL does not use the CMake build system I am relying on janbar/openssl-cmake Github repo for building the 1.1.1w version of OpenSSL. Here's what i have gotten so far,

cmake_minimum_required(VERSION 3.28)
set(PROJECT_NAME untitled)
project(${PROJECT_NAME} C)

set(CMAKE_C_STANDARD 11)

include(FetchContent)
FetchContent_Declare(
        curl
        URL "https://github.com/curl/curl/archive/refs/tags/curl-8_7_1.tar.gz"
        OVERRIDE_FIND_PACKAGE
)
FetchContent_Declare(
        openssl
        URL "https://github.com/janbar/openssl-cmake/archive/refs/tags/1.1.1w-20231130.tar.gz"
        OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(openssl)

set(BUILD_STATIC_LIBS ON CACHE INTERNAL "Build static libraries" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Switch off building shared libraries" FORCE)
set(ENABLE_WEBSOCKETS ON CACHE INTERNAL "Set to ON to enable EXPERIMENTAL websockets" FORCE)
set(BUILD_CURL_EXE OFF CACHE BOOL "Switch off building curl executable" FORCE)
set(BUILD_TESTING OFF CACHE INTERNAL "Switch off building curl tests" FORCE)
set(CURL_USE_OPENSSL ON CACHE INTERNAL "Use OpenSSL backend" FORCE)

FetchContent_MakeAvailable(curl)

add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} CURL::libcurl_static)

Using the Reload CMake Project button in the IDE I get the following output.

"C:\Users\ADMIN\AppData\Local\Programs\CLion Nova\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Users/ADMIN/AppData/Local/Programs/CLion Nova/bin/ninja/win/x64/ninja.exe" -G Ninja -S "D:\Github Projects\C\untitled1" -B "D:\Github Projects\C\untitled1\cmake-build-debug"
-- The C compiler identification is MSVC 19.35.32215.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Deprecation Warning at cmake-build-debug/_deps/openssl-src/CMakeLists.txt:26 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- The CXX compiler identification is MSVC 19.35.32215.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- OpenSSL version 1.1.1w
-- MSVC: using statically-linked runtime (/MT and /MTd).
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of long
-- Check size of long - done
-- Check size of long long
-- Check size of long long - done
-- Check size of int
-- Check size of int - done
-- Looking for fcntl.h
-- Looking for fcntl.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Found Threads: TRUE  
-- Using CMake version 3.28.1
-- curl version=[8.7.0-DEV]
-- Found Perl: C:/cygwin64/bin/perl.exe (found version "5.36.3") 
-- Looking for OPENSSL_IS_BORINGSSL
-- Looking for OPENSSL_IS_BORINGSSL - not found
-- Looking for OPENSSL_IS_AWSLC
-- Looking for OPENSSL_IS_AWSLC - not found
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR) 
-- Looking for SSL_set0_wbio
-- Looking for SSL_set0_wbio - not found
-- Looking for SSL_CTX_set_srp_username
-- Looking for SSL_CTX_set_srp_username - not found
-- Looking for idn2_lookup_ul in idn2
-- Looking for idn2_lookup_ul in idn2 - not found
-- Could NOT find LibPSL (missing: LIBPSL_LIBRARY LIBPSL_INCLUDE_DIR) 
-- Could NOT find LibSSH2 (missing: LIBSSH2_LIBRARY LIBSSH2_INCLUDE_DIR) 
-- Performing Test HAVE_WIN32_WINNT
-- Performing Test HAVE_WIN32_WINNT - Success
-- Found _WIN32_WINNT=0x0a00
-- Check size of size_t
-- Check size of size_t - done
-- Check size of ssize_t
-- Check size of ssize_t - failed
-- Check size of long long
-- Check size of long long - done
-- Check size of long
-- Check size of long - done
-- Check size of int
-- Check size of int - done
-- Check size of __int64
-- Check size of __int64 - done
-- Check size of time_t
-- Check size of time_t - done
-- Check size of ADDRESS_FAMILY
-- Check size of ADDRESS_FAMILY - done
-- Check size of off_t
-- Check size of off_t - done
-- Check size of curl_off_t
-- Check size of curl_off_t - done
-- Check size of curl_socket_t
-- Check size of curl_socket_t - done
-- Enabled features: SSL IPv6 UnixSockets AsynchDNS Largefile alt-svc HSTS NTLM HTTPS-proxy threadsafe
-- Enabled protocols: DICT FILE FTP FTPS GOPHER GOPHERS HTTP HTTPS IMAP IMAPS IPFS IPNS LDAP LDAPS MQTT POP3 POP3S RTSP SMB SMBS SMTP SMTPS TELNET TFTP WS WSS
-- Enabled SSL backends: OpenSSL
-- Configuring done (28.2s)
CMake Error at cmake-build-debug/_deps/curl-src/lib/CMakeLists.txt:128 (target_link_libraries):
  Target "libcurl_object" links to:

    OpenSSL::SSL

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.



CMake Error at cmake-build-debug/_deps/curl-src/lib/CMakeLists.txt:155 (target_link_libraries):
  Target "libcurl_static" links to:

    OpenSSL::SSL

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.



-- Generating done (0.2s)
CMake Generate step failed.  Build files cannot be regenerated correctly.

[Failed to reload]

It seems that OpenSSL gets built but the Github repo I use outputs ssl, crypto and uninstall modules? Idk what they are called. So my question is how do I

  1. Build OpenSSL from source into a static library
  2. Build libCURL from source and then ask it to statically link with OpenSSL that was just built
  3. Link everything using target_link_libraries

Upvotes: 0

Views: 719

Answers (0)

Related Questions