首页 > 解决方案 > CMake 在 Brew 上找不到 OpenSSL

问题描述

CMake 无法为我找到 OpenSSL。Brew 的 OpenSSL 位于/usr/local/Cellar/openssl/1.0.2s/. 我在 macOS Catalina 上。

我的CMakeLists.txt样子是这样的:

[...]

set(OPENSSL_ROOT_DIR /usr/local/Cellar/openssl/1.0.2s)

find_package(OpenSSL REQUIRED)
target_include_directories(myapp PUBLIC ${OPENSSL_INCLUDE_DIR})
message(STATUS "OpenSSL: Version ${OPENSSL_VERSION}")
message(STATUS "OpenSSL: include dir at ${OPENSSL_INCLUDE_DIR}")
message(STATUS "OpenSSL: libraries at ${OPENSSL_LIBRARIES} ${OPENSSL_SSL_LIBRARIES}")

[...]

target_link_libraries(myapp
        ${CMAKE_DL_LIBS}
        ${OPENSSL_LIBRARIES}
        ${OPENSSL_SSL_LIBRARIES}
        Qt5::Core
        )

输出:

-- OpenSSL: Version 1.0.2s
-- OpenSSL: include dir at /usr/local/Cellar/openssl/1.0.2s/include
-- OpenSSL: libraries at /usr/lib/libssl.dylib;/usr/lib/libcrypto.dylib /usr/lib/libssl.dylib

所以 CMake 找到了正确的包含目录,但是这些库是从我系统安装的 OpenSSL 中获取的?即使我按照OPENSSL_ROOT_DIR文档指定

为什么找不到这些文件?

/usr/local/Cellar/openssl/1.0.2s/lib/libcrypto.a
/usr/local/Cellar/openssl/1.0.2s/lib/pkgconfig/libssl.pc
/usr/local/Cellar/openssl/1.0.2s/lib/pkgconfig/libcrypto.pc
/usr/local/Cellar/openssl/1.0.2s/lib/libssl.dylib
/usr/local/Cellar/openssl/1.0.2s/lib/libcrypto.1.0.0.dylib
/usr/local/Cellar/openssl/1.0.2s/lib/libcrypto.dylib
/usr/local/Cellar/openssl/1.0.2s/lib/libssl.1.0.0.dylib
/usr/local/Cellar/openssl/1.0.2s/lib/libssl.a

如果我忽略这一点,并继续编译我的程序,我会得到:

ld: cannot link directly with /usr/lib/libcrypto.dylib for architecture x86_64

我尝试brew upgrade openssl了它的价值。

我从 CLion 运行 CMake,包括-DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=/Users/me/Qt/5.9.7/clang_64

标签: c++macoscmakeopenssl

解决方案


CMake 缓存是问题所在。在 CLion 中,我可以通过转到Tools -> CMake -> Reset cache & reload project. 在此之后,输出为:

-- OpenSSL: Version 1.0.2s
-- OpenSSL: include dir at /usr/local/Cellar/openssl/1.0.2s/include
-- OpenSSL: libraries at /usr/local/Cellar/openssl/1.0.2s/lib/libssl.dylib;/usr/local/Cellar/openssl/1.0.2s/lib/libcrypto.dylib /usr/local/Cellar/openssl/1.0.2s/lib/libssl.dylib

这似乎是正确的,并回答了我的问题。

另外,我还要做:brew switch openssl 1.0.2s正确编译。


推荐阅读