TARGET_OBJECTS的CMake通配符 [英] CMake Wildcard for TARGET_OBJECTS

查看:2049
本文介绍了TARGET_OBJECTS的CMake通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下目录结构

-  CMakeLists.txt
-  include
    -  a.hh
-  lib
    -  CMakeLists.txt
    -  a.cc
-  src
    -  CMakeLists.txt
    -  main.cc
-  test
    -  CMakeLists.txt
    -  test.cc

我正在使用CMake使用命令

I am using CMake for compiling test.cc using the command

add_executable(test test.cc $<TARGET_OBJECTS:A>)

和目标文件 Ao 被编译为

add_library(A OBJECT A.cc)

,但 $< TARGET_OBJECTS:A> 命令变得非常难看。有没有办法告诉CMake在lib文件夹中包含所有 .o 文件以链接所有内容?

but the $<TARGET_OBJECTS:A> command becomes ugly very quickly. Is there any way to tell CMake to include all .o files in the lib folder for linking everything?

推荐答案

将我的评论变成答案

没有像通配符这样的内置语法可以简化您的 add_executable(test test.cc $&命令。

There is no build-in syntax like wildcards to simplify your add_executable(test test.cc $<TARGET_OBJECTS:A>) command.

实际上-为了提高当前项目中处理对象库的便利性-我一直在修改CMake版本的源代码以扩展 target_link_libraries()接受对象库的方式与处理静态库或共享库的方式相同。

Actually - to improve the convenience of handling object libraries in my current project - I've gone all the way of modifying my CMake version's source code to extend the target_link_libraries() to accept object libraries the same way it does static or shared libraries.

但是还有其他方法可能会有所帮助您需要处理大量的对象库:

But there are other ways that may help you handling large amounts of object libraries:


  1. 您可以将对象库分组为中间静态库(但是以某种方式抵消了使用对象库):

  1. You could group object libraries into intermediate static libraries (but that somehow negates the effect of using object libraries):

add_library(A OBJECT a.cc)
add_library(B OBJECT b.cc)
add_library(AB $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>) 


  • 您可以扩展 add_executable()的功能,例如

    function(my_add_executable _target)
        foreach(_source IN ITEMS ${ARGN})
            if (NOT TARGET "${_source}")
                list(APPEND _source_list "${_source}")
            else()
                get_target_property(_type "${_source}" TYPE)
                if (_type STREQUAL "OBJECT_LIBRARY")
                    list(APPEND _source_list "$<TARGET_OBJECTS:${_source}>")
                else()
                    message(SEND_ERROR "my_add_executable: '${_source}' given as parameter is not a object-library target.")
                endif()
            endif()
        endforeach()
        add_executable(${_target} ${_source_list})
    endfunction(my_add_executable)
    

    然后可以混合源文件带有对象库的名称。为了避免造成太多混乱,我建议使用更具描述性的对象库名称(例如,通过添加 Objs后缀):

    Then you could mix your source files with names of object libraries. To prevent to much confusion I would recommend using more descriptive object library names (e.g. by adding a "Objs" suffix):

    add_library(AObjs OBJECT a.cc)
    ...
    my_add_executable(testA test.cc AObjs)
    


  • 参考

    • CMake properties and expanding generator expressions

    这篇关于TARGET_OBJECTS的CMake通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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