CMake 编译错误:Objects of target “ssl“ referenced but is not an OBJECT library

本文介绍了在更新libquic并使用最新boringssl代码时遇到的编译错误及其解决方法,包括调整CMakeLists.txt中的设置以确保正确链接,并解决了因依赖项配置不当导致的未定义引用问题。

1、问题描述

libquic 支持最新 chromium 代码的过程中,发现 boringssl 库编译错误。

2、解决过程

  • 下载最新的 boringssl 代码,在本地成功编译,因此可尝试用最新的 boringssl 代码替换现有代码来编译,但是在编译 libquic 时,出现如下错误:
CMake Error at CMakeLists.txt:925 (add_library):
  Error evaluating generator expression:

    $<TARGET_OBJECTS:ssl>

  Objects of target "ssl" referenced but is not an OBJECT library.

CMake Error at CMakeLists.txt:925 (add_library):
  No SOURCES given to target: quic

查看 libquic 的 CMakeLists.txt 对应内容:

923 add_subdirectory(src/third_party/boringssl/src)
924 
925 add_library (
926     quic
927 
928     STATIC
929 
930     ${BASE_SOURCES}
931     ${NET_SOURCES}
932     ${CRYPTO_SOURCES}
933     ${URL_SOURCES}
934     ${ZLIB_SOURCES}
935     ${PROTOBUF_LITE_SOURCES}
936     ${LIBEVENT_SOURCES}
937     # boringssl 编译出的目标文件
938     $<TARGET_OBJECTS:ssl>
939     $<TARGET_OBJECTS:crypto>
940     $<TARGET_OBJECTS:fiat>
941 
942     src/third_party/modp_b64/modp_b64.cc
943 )

梳理 add_subdirectory 及 TARGET_OBJECTS 的原理;再与原来 boringssl 的 CMakeLists.txt 比较发现,目标库 ssl、crypto 在输出语句 add_library 中均少了一个 OBJECT 关键字,
而少了它意味着无法在 libquic 中访问这两个依赖库,因为 TARGET_OBJECTS 只能引用 Object 库,即 ssl 等需要被被编译为 OBJECT 目标库。

在 ssl 的 CMakeLists.txt 中添加 OBJECT 即可解决前面所述错误。

随后出现 link crypto_test、ssl_test 的错误:

/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_ec_asn1.c:222: undefined reference to `EC_KEY_free'
CMakeFiles/crypto.dir/evp/p_ec_asn1.c.o: In function `eckey_opaque':
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_ec_asn1.c:225: undefined reference to `EC_KEY_is_opaque'
CMakeFiles/crypto.dir/evp/p_rsa.c.o: In function `pkey_rsa_copy':
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:126: undefined reference to `BN_dup'
CMakeFiles/crypto.dir/evp/p_rsa.c.o: In function `pkey_rsa_cleanup':
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:154: undefined reference to `BN_free'
CMakeFiles/crypto.dir/evp/p_rsa.c.o: In function `pkey_rsa_sign':
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:191: undefined reference to `EVP_MD_type'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:191: undefined reference to `RSA_sign'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:198: undefined reference to `RSA_sign_pss_mgf1'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:206: undefined reference to `RSA_sign_raw'
CMakeFiles/crypto.dir/evp/p_rsa.c.o: In function `pkey_rsa_verify':
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:218: undefined reference to `EVP_MD_type'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:218: undefined reference to `RSA_verify'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:221: undefined reference to `RSA_verify_pss_mgf1'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:232: undefined reference to `RSA_verify_raw'
CMakeFiles/crypto.dir/evp/p_rsa.c.o: In function `pkey_rsa_verify_recover':
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:260: undefined reference to `RSA_verify_raw'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:270: undefined reference to `EVP_MD_size'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:275: undefined reference to `EVP_MD_type'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:275: undefined reference to `RSA_add_pkcs1_prefix'
/home/colin/work-dir/cpp-proj/libquic/src/third_party/boringssl/src/crypto/evp/p_rsa.c:283: undefined reference to `RSA_verify_raw'

该错误出现的直接原因是将 crypto、ssl 的 CMakeLists.txt 中添加了 OBJECT 关键字导致的。
上面这些出错的未定义引用全部来自 crypto 模块的子模块 fipsmodule,而 cmake 无法将一个 OBJECT library 编译进另一个 OBJECT library,只能编译进 STATIC library,因此 fipsmodule 并未被编译进 crypto,也就不会编译进 libquic,

解决办法是直接在 libquic 的中增加对 fipsmodule 的依赖,即添加: $<TARGET_OBJECTS:fipsmodule>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值