验证库在CMake脚本中的target_link_libraries之前可用 [英] Verify library is available before target_link_libraries in CMake Script

查看:106
本文介绍了验证库在CMake脚本中的target_link_libraries之前可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型项目,其中包含多个构建目标,它们之间具有依赖性.结构类似于这样:

I have a large project consisting of several build targets with dependencies between them. The structure resembles sth like this:

Application <-- Library I <--- Library II
            <-- Library III <---|
            <-- Library IV

存在多个此类应用程序,它们使用分布在不同库中的共享代码.

Multiple such Applications exist, which use shared code distributed across different libraries.

在项目中,CMake用于确保正确的包含路径和各种库之间的链接.

Within the project CMake is used to ensure correct include paths and linking among the various libraries.

使用add_library("Library II")设置库,随后另一个项目使用target_link_libraries("Library I""Library II")获得依赖项.

The libraries are setup using add_library("Library II"), subsequently the other project get the dependencies using target_link_libraries("Library I" "Library II").

这在大多数情况下都有效.但是,有时找不到某些依赖项.我怀疑在某些情况下图书馆I"是未知的.但是,如果不知道库,target_link_libraries()不会引发错误.该错误仅在编译/链接时出现.

This works in most of the cases. However, sometimes certain dependencies are not found. I have the suspicion that in some cases "Library I" is not known. However, target_link_libraries() does not throw an error if a library is not known. The error will only appear when compiling / linking.

我想在运行cmake时验证是否已找到所有库.如果在那个阶段尚不知道某事,我想抛出一个错误并通知开发人员.

I would like to verify when running cmake already that all libraries are found. If sth is not known at that stage I would like to throw an error and inform the developer.

我尝试使用find_libraries(),但据我了解,这会查找某个文件.但是,就我而言,该文件只会在编译阶段进行编译,因此在运行cmake时该文件不存在.

I tried using find_libraries() but in my understanding this looks for a certain file. However, in my case the file will only be compiled at compile stage, so the file does not exist when running cmake.

推荐答案

检查库目标,而不是文件:

if(not TARGET library_2)
   message(SEND_ERROR "Attempt to link to non-existent library 'library_2'.")
endif()
target_link_libraries(library_1 library_2)

另请参见该问题关于检查目标.

See also that question about checking the target.

请注意,这种方法仅在以下情况下有效

Note, that this approach will work only when

add_library(library_2)

先于

target_link_libraries(library_1 library_2)

CMake允许(并正确处理)相反的顺序,但是我发现在库创建和链接之间要求直接顺序是一种很好的样式.

CMake allows (and correctly processes) opposite order, but I find it to be good style to require direct order between a library creation and linking.

这篇关于验证库在CMake脚本中的target_link_libraries之前可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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