在CMake中静态链接OpenSSL Crypto [英] Static linking of OpenSSL Crypto in CMake

查看:296
本文介绍了在CMake中静态链接OpenSSL Crypto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为MIPS设备制作交叉编译的OpenSSL.我尝试按照文档进行操作.将 OPENSSL_USE_STATIC_LIBS 设置为true,然后将 target_link_libraries 设置为所需的库文件.

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:

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程序:

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.

已经查看了在CMake中链接静态OpenSSL加密库但是它并没有告诉我任何新的东西.

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

根据回复更新CMakeLists.txt文件:CMakeLists.txt:

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)

输出:

-- 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

编辑繁荣:如果您(以后的人)遇到dlopen的 undefined引用,则将以下内容添加到我的 CMakeLists.txt 文件

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})

推荐答案

设置为TRUE,变量 OPENSSL_USE_STATIC_LIBS 强制 find_package(OpenSSL)搜索静态库.因此,此变量仅适用于该调用,并且如果您使用其结果,则可以使用该变量:

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)


如果未设置 OPENSSL_USE_STATIC_LIBS 的情况下已经执行了 cmake ,则需要删除CMake缓存(构建目录下的 CMakeCache.txt )在进行新尝试之前.否则,将使用已找到(共享!)的库,并且不会进行任何重新搜索.


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.

这篇关于在CMake中静态链接OpenSSL Crypto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆