使用add_subdirectory构建glog和gflags的问题

问题描述

使用cmake构建项目,希望使用glog记录日志,但是glog默认依赖于gflags,所以将其作为子模块添加到主项目,代码如下:

add_subdirectory(../third_party/gflags)
add_subdirectory(../third_party/glog)

出现问题:

CMake Error: install(EXPORT "glog-targets" ...) includes target "glog" which requires target "gflags_nothreads_static" that is not in any export set.
CMake Error in D:/my/MiniNN/third_party/glog/CMakeLists.txt:
  export called with target "glog" which requires target
  "gflags_nothreads_static" that is not in this export set, but in multiple
  other export sets:
  D:/my/MiniNN/src/build/third_party/gflags/gflags-nonamespace-targets.cmake,
  D:/my/MiniNN/src/build/third_party/gflags/gflags-targets.cmake.

  An exported target cannot depend upon another target which is exported
  multiple times.  Consider consolidating the exports of the
  "gflags_nothreads_static" target to a single export.

问题原因

实际是出现了2个导出集,chatgpt也没能给出一个可行的办法,上网查了查,很多讨论:

其中,单独编译glog不通过,因为依赖gtest,梳理好依赖之后,又依赖gflags,有点麻烦,再添加gflags,就出现了上面的问题,整的有点麻烦。

单独编译gflags,是可以编译通过的,gflags被当作子模块编译的时候,自己也提到了:

## When this project is a subproject (GFLAGS_IS_SUBPROJECT is TRUE), the default
## settings are such that only the static single-threaded library is built without
## installation of the gflags files. The "gflags::gflags" target is in this case an ALIAS
## library target for the "gflags_nothreads_static" library target. Targets which
## depend on the gflags library should link to the "gflags::gflags" library target.

问题解决

有以下几个方法供参考:

  1. 使用ExternalProject
  2. 设置关闭gflags:
set(WITH_GFLAGS OFF)
add_subdirectory(../third_party/glog)

参考资料

以上链接

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值