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

查看:2574
本文介绍了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天全站免登陆