CMake - 链接到从ExternalProject_add()下载的库
我正在尝试使用ExternalProject_add()来下载/安装依赖项。它安装得很好,但我不知道下载库后如何实际链接它们。
我想在刚刚下载的库上调用 target_link_libraries() ,但库的路径会因系统而异。
如果这是系统依赖项,我可以只调用 find_package() - 但这些包没有安装在默认搜索路径上。我认为您不能在模块模式下指定 find_package 的搜索路径。
这是我的 CMakeLists.txt 的一个片段,该片段不起作用:
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})
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.
I want to call target_link_libraries() on the library that was just downloaded, but the path to the library will vary by system.
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.
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})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为您正在下载外部项目,所以您已经知道所有内容在哪里,因为您刚刚下载了它,因此不需要“查找”。
我让它与 add_library 一起工作。这是我实际有效的代码:
从那里开始,依赖于它的每个程序都需要一个
add_dependency
,当然还有target_link_libraries
。例如:add_dependency 使其在尝试包含所需的目录之前等待。
这对我有用。与 make -j4 一起使用。正确获取所有依赖项。
Because you're downloading the external project, you already know where everything is because you just downloaded it, so it doesn't need 'finding'.
I got it working with add_library. This is my actual code that works:
From there, each program that depends on it needs a
add_dependencies
and of coursetarget_link_libraries
. Eg:The add_dependencies makes it wait before trying to include the dirs needed.
That does the trick for me. Works with make -j4. Get's all the dependencies right.
要扩展上面的 DLRdave 答案,您不需要手动设置静态库的前缀和后缀,因为 CMAKE 为每个平台提供了正确的变量。
有关详细信息,请参阅 CMake 有用变量。
例如:
To expand on DLRdave answer above you don't need set manually prefixes and suffixes for static libraries because CMAKE provides variables with the right ones for each platform.
See CMake Useful Variables for more information.
For example:
当您使用ExternalProject_Add时,您不能使用find_package,因为当CMake运行来配置外部项目时,没有什么可找到的。
因此,如果库位置因平台而异,您将需要基于您的平台的条件逻辑。 (我不知道这里的protobuf的库或结构,所以这只是一个例子,但它应该让你朝着正确的方向前进......)像这样的东西:
当然,这比使用find_package不太方便。如果您可以使用预构建/预安装的软件包,则应该使用,以便可以使用 find_package。但是,如果您必须从源代码构建其他包作为项目的一部分,ExternalProject_Add 很有用,即使它无法为您抽象出所有详细信息。
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.
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:
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.
您可以使用link_directories命令链接特定目录中的库。在您的情况下,您的外部项目的目录是构建的。
将输出目录添加到搜索路径:
确保在指定链接目录之后添加可执行文件:
指定项目应链接到的库:
不要忘记依赖外部项目:
You can use the link_directories command to link libraries within a specific directory. In your case the directory were your external project is build.
Add the output directory to the search path:
Make sure to add the executable after specifying the link directory:
Specify the libraries your project should be linked to:
Don't forget to depend on the external project:
您可以使用另一个习惯用法来解决此问题:
您可以在 https://github.com/biometrics/likely 中查看此习惯用法的实际应用。
Another idiom you can use to solve this problem:
You can see this idiom in action in https://github.com/biometrics/likely.
您可以将
find_package
与ExternalProject_add
一起使用,如以下代码片段:https://gist.github.com/amir-saniyan/4339e6f3ef109c75eda8018f7d5192a7
You can use
find_package
withExternalProject_add
like the following code snippet:https://gist.github.com/amir-saniyan/4339e6f3ef109c75eda8018f7d5192a7