gpt4 book ai didi

cmake - OBJECT 库上的可传递 target_include_directories

转载 作者:行者123 更新时间:2023-12-03 14:14:57 30 4
gpt4 key购买 nike

这是来自 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>)

现在,这一切正常,两个库都生成了。但是,当我尝试使用它时遇到了一个问题:
add_executable(bar src/main.cpp)
target_link_libraries(bar foo)

目标 bar不编译,因为来自 foo-object 的包含目录不会传播。如果我添加 target_include_directories直接上 foo同样,一切都会很好地编译。

我怎样才能做到这两者 foofoo_static自动使用(并根据它们转发到内容)包含来自 foo-object 的目录?

最佳答案

嗯,目前我想出了以下几点:

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})

但是来吧,一定有更好的方法:/

关于cmake - OBJECT 库上的可传递 target_include_directories,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38832528/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com