OBJECT库上的传递性target_include_directories [英] Transitive target_include_directories on OBJECT libraries

查看:154
本文介绍了OBJECT库上的传递性target_include_directories的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是make CMakeLists.txt中的代码段:

Here is snippet from make CMakeLists.txt:

add_library(foo-object OBJECT src/foo.cpp)
target_include_directories(foo-object PUBLIC include)
add_library(foo SHARED $<TARGET_OBJECTS:${PROJECT_NAME}-object>)
add_library(foo_static STATIC $<TARGET_OBJECTS:${PROJECT_NAME}-object>)

现在,这一切正常,两个库都生成了。但是,当我尝试使用它时遇到问题:

Now, this all works fine, both libraries are generated. However I have a problem when I try to use it:

add_executable(bar src/main.cpp)
target_link_libraries(bar foo)

目标 bar 不t编译,因为不传播foo-object的include目录。如果我也直接在 foo 上添加 target_include_directories ,一切都可以编译。

Target bar doesn't compile, because include directories from foo-object are not propagated. If I add target_include_directories directly on foo as well, everything will compile fine.

如何使 foo foo_static 都自动使用(并根据它们转发内容)包括 foo-object

How can I make both foo and foo_static automatically use (and forward to stuff depending on them) include directories from foo-object?

推荐答案

Hm中的目录,我想到以下内容:

Hm, at the moment I came up with following:

add_library(foo-object OBJECT src/foo.cpp)
target_include_directories(foo-object PUBLIC include)

get_property(object_include_dirs TARGET foo-object PROPERTY INCLUDE_DIRECTORIES)
get_property(object_link_libs TARGET foo-object PROPERTY LINK_LIBRARIES)

add_library(foo SHARED $<TARGET_OBJECTS:${PROJECT_NAME}-object>)
target_include_directories(foo PUBLIC ${object_include_dirs})
target_link_libraries(foo PUBLIC ${object_link_libs})

add_library(foo_static STATIC $<TARGET_OBJECTS:${PROJECT_NAME}-object>)
target_include_directories(foo_static PUBLIC ${object_include_dirs})
target_link_libraries(foo_static PUBLIC ${object_link_libs})

但是加油,必须有更好的方法:/

but come on, there must be better way :/

这篇关于OBJECT库上的传递性target_include_directories的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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