CMake - 链接到从 ExternalProject_add() 下载的库 [英] CMake - linking to library downloaded from ExternalProject_add()

查看:9
本文介绍了CMake - 链接到从 ExternalProject_add() 下载的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ExternalProject_add() 下载/安装依赖项.它安装得很好,但我不知道如何在下载后实际链接这些库.

I am trying to use ExternalProject_add() to download/install dependencies. It installs fine, but I can't figure out how to actually link the libraries after they are downloaded.

我想对刚刚下载的库调用 target_link_libraries(),但是库的路径会因系统而异.

I want to call target_link_libraries() on the library that was just downloaded, but the path to the library will vary by system.

如果这是系统依赖项,我可以调用 find_package() - 但这些包并未安装在默认搜索路径上.我认为您不能在模块模式下为 find_package 指定搜索路径.

If this were a system dependency, I could just call find_package() - but the packages weren't installed on the default search path. I don't think you can specify a search path for find_package in module mode.

这是我的 CMakeLists.txt 的一个片段,它不起作用:

Here's a snippet of my CMakeLists.txt that doesn't work:

ExternalProject_Add(
protobuf
URL http://protobuf.googlecode.com/files/protobuf-2.4.1.tar.gz
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
PREFIX ${MYPROJ_SOURCE_DIR}/dependencies
)
find_package(protobuf REQUIRED)
set(LIBS ${LIBS} ${PROTOBUF_LIBRARIES})
target_link_libraries (mybinary ${LIBS})

推荐答案

当你使用 ExternalProject_Add 时,你不能使用 find_package,因为当 CMake 运行以配置外部项目时,没有任何东西可以找到.

When you're using ExternalProject_Add, you can't use find_package, since there's nothing to find when CMake runs to configure the outer project.

因此,如果库位置因平台而异,您将需要基于您的平台的条件逻辑.(我不知道 protobuf 的库或这里的结构,所以这只是一个例子,但它应该让你朝着正确的方向前进......)像这样:

So, if library locations vary by platform you will need conditional logic based on your platform. (I don't know protobuf's libraries or structure here, so this is just an example, but it should get you headed in the right direction...) Something like this:

if(WIN32)
  set(PROTOBUF_LIB_DIR "${MYPROJ_SOURCE_DIR}/dependencies/win"
  set(prefix "")
  set(suffix ".lib")
elseif(APPLE)
  set(PROTOBUF_LIB_DIR "${MYPROJ_SOURCE_DIR}/dependencies/mac"
  set(prefix "lib")
  set(suffix ".a")
else()
  set(PROTOBUF_LIB_DIR "${MYPROJ_SOURCE_DIR}/dependencies/linux"
  set(prefix "lib")
  set(suffix ".a")
endif()

set(PROTOBUF_LIBRARIES
  "${PROTOBUF_LIB_DIR}/${prefix}protobufLib1${suffix}"
  "${PROTOBUF_LIB_DIR}/${prefix}protobufLib2${suffix}")

当然,这不如使用 find_package 方便.如果您可以使用预构建/预安装的软件包,那么您应该使用 find_package.如果您必须从源代码构建另一个包作为项目的一部分,那么 ExternalProject_Add 很有用,即使它无法为您抽象出所有细节.

Granted, this is less convenient than using find_package. If you can use a pre-built/pre-installed package, you should, so that you can use find_package. If you must build the other package from source code as part of your project, though, ExternalProject_Add is useful, even though it is unable to abstract away all the details for you.

这篇关于CMake - 链接到从 ExternalProject_add() 下载的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆