You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为CMake配置类似CFLAGS的参数以查找本地依赖(如OpenSSL)?

问题描述

尝试安装C++ Web框架drogon,执行的安装步骤如下:

cd $WORK_PATH
git clone https://github.com/drogonframework/drogon
cd drogon
git submodule update --init
mkdir build
cd build
cmake ..
make && sudo make install

为实现本地安装,添加了参数:cmake --install-prefix=$HOME/local,但make过程中出现链接错误:

/bin/ld: ../trantor/libtrantor.a(OpenSSLProvider.cc.o): in function `OpenSSLProvider::processHandshake()':
OpenSSLProvider.cc:(.text._ZN15OpenSSLProvider16processHandshakeEv[_ZN15OpenSSLProvider16processHandshakeEv]+0x504): undefined reference to `SSL_get1_peer_certificate'
/bin/ld: ../trantor/libtrantor.a(openssl.cc.o): in function `trantor::utils::md5(void const*, unsigned long)':
openssl.cc:(.text+0x35): undefined reference to `EVP_MD_fetch'
/bin/ld: openssl.cc:(.text+0xa1): undefined reference to `EVP_MD_free'
/bin/ld: ../trantor/libtrantor.a(openssl.cc.o): in function `trantor::utils::sha1(void const*, unsigned long)':
openssl.cc:(.text+0xfc): undefined reference to `EVP_MD_fetch'
/bin/ld: openssl.cc:(.text+0x168): undefined reference to `EVP_MD_free'
/bin/ld: ../trantor/libtrantor.a(openssl.cc.o): in function `trantor::utils::sha256(void const*, unsigned long)':
openssl.cc:(.text+0x1c0): undefined reference to `EVP_MD_fetch'
/bin/ld: openssl.cc:(.text+0x22c): undefined reference to `EVP_MD_free'
/bin/ld: ../trantor/libtrantor.a(openssl.cc.o): in function `trantor::utils::sha3(void const*, unsigned long)':
openssl.cc:(.text+0x284): undefined reference to `EVP_MD_fetch'
/bin/ld: openssl.cc:(.text+0x2f0): undefined reference to `EVP_MD_free'
/bin/ld: ../trantor/libtrantor.a(openssl.cc.o): in function `trantor::utils::blake2b(void const*, unsigned long)':
openssl.cc:(.text+0x348): undefined reference to `EVP_MD_fetch'
/bin/ld: openssl.cc:(.text+0x3b4): undefined reference to `EVP_MD_free'
collect2: error: ld returned 1 exit status
make[2]: *** [examples/CMakeFiles/client.dir/build.make:105: examples/client] Error 1
make[1]: *** [CMakeFiles/Makefile2:293: examples/CMakeFiles/client.dir/all] Error 2

根据CMake输出,错误源于系统OpenSSL版本过旧:

Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.1.1f")  

已在$HOME/.local下安装最新版OpenSSL,但重新安装drogon时仍出现相同错误,CMake仍检测到系统旧版OpenSSL。需了解如何让CMake查找本地依赖,是否存在类似./configure中CFLAGS="-I/usr/local/include"的配置方法。

解决方案

CMake提供多种方式指定本地依赖路径,对应./configure的CFLAGS/LDFLAGS配置,以下是针对OpenSSL的具体方法:

方法1:通过CMAKE_PREFIX_PATH指定依赖根目录

CMAKE_PREFIX_PATH是CMake查找依赖的优先路径,将本地OpenSSL的安装根目录加入该变量即可:

# 方式1:先设置环境变量再执行cmake
export CMAKE_PREFIX_PATH=$HOME/.local:$CMAKE_PREFIX_PATH
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/local

# 方式2:直接在cmake命令中传递参数
cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/local -DCMAKE_PREFIX_PATH=$HOME/.local

方法2:直接指定OpenSSL的头文件和库路径

如果方法1不生效,可直接指定OpenSSL的具体路径参数:

cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/local \
  -DOPENSSL_INCLUDE_DIR=$HOME/.local/include \
  -DOPENSSL_CRYPTO_LIBRARY=$HOME/.local/lib/libcrypto.so \
  -DOPENSSL_SSL_LIBRARY=$HOME/.local/lib/libssl.so

注意:如果你的系统是64位,库路径可能是$HOME/.local/lib64,需根据实际安装情况调整。

关键注意事项

  1. 清空build目录再重新配置:CMake会缓存之前的依赖检测结果,不清空的话新参数不会生效:
cd build
rm -rf ./*
  1. 确保本地OpenSSL安装完整:如果是源码编译安装,需确保指定了正确的prefix,编译命令示例:
cd openssl-source-directory
./Configure --prefix=$HOME/.local shared zlib
make && make install

内容的提问来源于stack exchange,提问作者Dummy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:57:51