导入目标库和接口库之间有什么区别? [英] What are the differences between IMPORTED target and INTERFACE libraries?

查看:414
本文介绍了导入目标库和接口库之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,接口库就像 Visual Studio属性表一样,非常有用。我们可以使用它来链接静态库并传播属性。

As I understand it INTERFACE libraries are like Visual Studio property sheets, so very useful. We can use it to link with static libs and propagate properties.

但是导入目标困扰着我:我看不到只能使用导入目标才能解决的问题。

But IMPORTED targets bother me : I can't see a problem which can be solved with IMPORTED target only.

推荐答案

创建导入的目标时,您在告诉CMake:我有这个{静态库|共享库|模块库|可执行文件}已在磁盘上的此位置构建。我希望能够像对待由自己的构建系统构建的目标一样对待它,因此请注意,当我说 ImportedTargetName 时,它应该引用磁盘上的该二进制文件(带有

When you create an imported target, you're telling CMake: I have this { static library | shared library | module library | executable } already built in this location on disk. I want to be able to treat it just like a target built by my own buildsystem, so take note that when I say ImportedTargetName, it should refer to that binary on disk (with the associated import lib if applicable, and so on).

创建接口库时,您在告诉CMake:我有这组属性(包括目录等)。 。)客户端可以使用的客户端,因此,如果它们链接到我的接口库,请将这些属性传播给他们。

When you create an interface library, you're telling CMake: I have this set of properties (include directories etc.) which clients can use, so if they "link" to my interface library, please propagate these properties to them.

根本的区别是不支持接口库根据磁盘上的任何内容,它们只是一组需求/属性。您可以可以根据需要在接口库上设置 INTERFACE_LINK_LIBRARIES 属性,但这并不是它们的初衷。它们将封装客户端消耗的属性,并且主要对诸如C ++中的仅标头库之类的东西有意义。

The fundamental difference is that interface libraries are not backed by anything on disk, they're just a set of requirements/properties. You can set the INTERFACE_LINK_LIBRARIES property on an interface library if you really want to, but that's not really what they were designed for. They're to encapsulate client-consumable properties, and make sense primarily for things like header-only libraries in C++.

还要注意,接口库是-没有接口可执行文件,但是您确实可以导入可执行文件。例如。 Bison的程序包配置文件可以为Bison可执行文件定义导入的目标,然后您的项目可以将其用于自定义命令:

Also notice that an interface library is a library—there's no such thing as an interface executable, but you can indeed have imported executables. E.g. a package config file for Bison could define an imported target for the Bison executable, and your project could then use it for custom commands:

# In Bison package config file:
add_executable(Bison IMPORTED)
set_property(TARGET Bison PROPERTY IMPORTED_LOCATION ...)

# In your project:
find_package(Bison)
add_custom_command(
  OUTPUT parser.c
  COMMAND Bison tab.y -o parser.c
  DEPENDS tab.y
  ...
)

(野牛只是作为您可能要在自定义命令中使用的示例,命令行可能不合适)。

(Bison is used just as an example of something you might want to use in custom commands, and the command line is probably not right for it).

这篇关于导入目标库和接口库之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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