装修公司做网站的好处,阿里巴巴上做英文网站一年多少钱,深圳罗湖企业网站,网站二级域名解析在 CMake 中#xff0c;public、private和 interface是用来指定目标属性的作用域的关键字#xff0c;这三个有什么区别呢#xff1f;这些关键字用于控制属性的可见性和传递性#xff0c;影响了目标之间的依赖关系和属性传递。
public
如果在一个目标上使用 public关键字时…在 CMake 中public、private和 interface是用来指定目标属性的作用域的关键字这三个有什么区别呢这些关键字用于控制属性的可见性和传递性影响了目标之间的依赖关系和属性传递。
public
如果在一个目标上使用 public关键字时该属性会传递给依赖于该目标的其他目标。例如在目标 A 上设置了一个 public属性然后将目标 A 作为目标 B 的依赖项那么目标 B 也会继承目标 A 上设置的 public属性目标B再作为目标C的依赖项的时候目标C对目标A是可见的。
private
如果在一个目标上使用 private关键字时该属性只会应用于当前目标不会传递给依赖项。例如在目标 A 上设置了一个 private属性该属性不会传递给依赖于目标 A 的其他目标。
interface
如果在一个目标上使用 interface 关键字时该属性会传递给依赖于当前目标的其他目标但不会应用于当前目标。例如在目标 A 上设置了一个 interface属性该属性会传递给依赖于目标 A 的其他目标但不会应用于目标 A 本身。
附上官方文档的一个例子Transitive Usage Requirements来看这里删除了一些无关的定义宏CMake的语句清晰一点
add_library(archive archive.cpp)add_library(serialization serialization.cpp)add_library(archiveExtras extras.cpp)
target_link_libraries(archiveExtras PUBLIC archive)
target_link_libraries(archiveExtras PRIVATE serialization)add_executable(consumer consumer.cpp)target_link_libraries(consumer archiveExtras)因为archive 是archiveExtras的PUBLIC依赖项所以它的符号在编译的时候也会传播给consumer 。 因为serialization 是archiveExtras的PRIVATE依赖项所以它的符号在编译的时候不会传播到consumer 。
Generally, a dependency should be specified in a use of target_link_libraries() with the PRIVATE keyword if it is used by only the implementation of a library, and not in the header files. If a dependency is additionally used in the header files of a library (e.g. for class inheritance), then it should be specified as a PUBLIC dependency. A dependency which is not used by the implementation of a library, but only by its headers should be specified as an INTERFACE dependency.
讲了target_link_libraries() 怎么使用public/private/interface关键字
【privatecpp使用依赖项hpp不使用依赖项】如果依赖项仅由库的实现使用而不是在头文件中使用使用private。【publiccpp使用依赖项hpp使用依赖项】如果在库的头文件中额外使用了依赖项库的视线也使用了依赖项例如用于类继承使用public。【interfacecpp不使用依赖项hpp使用依赖项】如果库的实现不使用依赖项而仅由其库的头文件使用的依赖项应指定为interface。