gpt4 book ai didi

cmake - CMake 中 OpenSSL Crypto 的静态链接

转载 作者:行者123 更新时间:2023-12-05 00:58:58 30 4
gpt4 key购买 nike

我需要为 MIPS 设备制作一个交叉编译的 OpenSSL。我已经尝试按照文档进行操作。将 OPENSSL_USE_STATIC_LIBS 设置为 true,并将 target_link_libraries 设置为您需要的库文件。

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)

它编译良好,没有警告,但检查生成的二进制文件告诉我它仍然是共享库。

readelf -d 程序:

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]

我不明白我做错了什么。

编辑:已经看过 Linking statically OpenSSL crypto library in CMake但它并没有告诉我任何新的东西。

编辑 2:根据回复更新了 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})

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

编辑繁荣:如果你, future 的人,遇到 undefined reference to dlopen,我将以下内容添加到我的 CMakeLists.txt 文件

target_link_libraries(program ${CMAKE_DL_LIBS})

最佳答案

设置为 TRUE,变量 OPENSSL_USE_STATIC_LIBS 强制 find_package(OpenSSL) 搜索静态库。所以这个变量只适用于那个调用,如果你使用它的结果:

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

如果你已经执行了cmake而没有设置OPENSSL_USE_STATIC_LIBS,那么你需要移除CMake缓存(CMakeCache.txt在build目录下)在新的尝试之前。否则,将使用已找到(共享!)的库,并且不会执行重新搜索。

关于cmake - CMake 中 OpenSSL Crypto 的静态链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55165172/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com