静态编译OpenSSL并作为CMake第三方库

编译环境

避免与现在的环境冲突,在Docker中创建一个容器(镜像),使用Ubuntu 18.04系统

docker pull ubuntu
docker run -it ubuntu bash

安装构建工具包

apt update
apt install build-essential

获取openssl 1.1.0h。1.1.0是目前的稳定版本

wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz

编译参数

./Configure linux-x86_64 --prefix=/tmp/openssl 
--openssldir=/tmp/openssl/ no-weak-ssl-ciphers enable-ec_nistp_64_gcc_128

gcc128位选项,需要从以下命令的结果中决定是否启用

gcc -dM -E - 

CMake第三方软件包编译

include(ExternalProject)

### OpenSSL 1.1.0h
ExternalProject_Add(OpenSSL PREFIX openssl
        URL ${CMAKE_CURRENT_SOURCE_DIR}/openssl-1.1.0h.tar.gz
        URL_MD5 5271477e4d93f4ea032b665ef095ff24
        CONFIGURE_COMMAND ./Configure linux-x86_64 --prefix= --openssldir=/lib/ssl
        no-weak-ssl-ciphers enable-ec_nistp_64_gcc_128 no-shared
        BUILD_IN_SOURCE 1)

ExternalProject_Get_Property(OpenSSL INSTALL_DIR)
set(OPENSSL_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
set(OPENSSL_LINK_DIRECTORIES ${INSTALL_DIR}/lib)
file(MAKE_DIRECTORY ${INSTALL_DIR}/include)

add_library(openssl-crypto-static STATIC IMPORTED GLOBAL)
set_property(TARGET openssl-crypto-static PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libcrypto.a)
set_property(TARGET openssl-crypto-static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)

add_library(openssl-ssl-static STATIC IMPORTED GLOBAL)
set_property(TARGET openssl-ssl-static PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libssl.a)
set_property(TARGET openssl-ssl-static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)

注意同属于一个工程的其他软件包,如使用了OpenSSL,也要链接这个版本的库文件、头文件,而不是使用操作系统自带的OpenSSL,例如,libevent

ExternalProject_Add(libevent PREFIX libevent
        URL ${CMAKE_CURRENT_SOURCE_DIR}/libevent-2.1.8-stable.tar.gz
        URL_MD5 f3eeaed018542963b7d2416ef1135ecc
        CONFIGURE_COMMAND ./configure --prefix= --disable-shared
        BUILD_COMMAND make "CPPFLAGS=-I${OPENSSL_INCLUDE_DIRECTORIES}" "LDFLAGS=-L${OPENSSL_LINK_DIRECTORIES} -ldl"
        BUILD_IN_SOURCE 1)

ExternalProject_Get_Property(libevent INSTALL_DIR)
file(MAKE_DIRECTORY ${INSTALL_DIR}/include)

add_library(libevent-static STATIC IMPORTED GLOBAL)
set_property(TARGET libevent-static PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libevent.a)
set_property(TARGET libevent-static PROPERTY IMPORTED_INTERFACE_LINK_LIBRARIES pthread crypto)
set_property(TARGET libevent-static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)

add_library(libevent-static-openssl STATIC IMPORTED GLOBAL)
set_property(TARGET libevent-static-openssl PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libevent_openssl.a)
set_property(TARGET libevent-static-openssl PROPERTY IMPORTED_INTERFACE_LINK_LIBRARIES pthread crypto)
set_property(TARGET libevent-static-openssl PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)

add_library(libevent-static-pthreads STATIC IMPORTED GLOBAL)
set_property(TARGET libevent-static-pthreads PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libevent_pthreads.a)
set_property(TARGET libevent-static-pthreads PROPERTY IMPORTED_INTERFACE_LINK_LIBRARIES pthread crypto)
set_property(TARGET libevent-static-pthreads PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)

增加了CPPFLAGS、LDFLAGS,以便在编译、链接时使用静态编译的OpenSSL。

参考文献

在 Ubuntu 上打包安装新版 OpenSSL

Compilation and Installation - OpenSSLWiki

你可能感兴趣的:(openssl)