CMake:如果任何可执行目标都需要,则构建目标库 [英] CMake: build target library if any of the executable targets needs it

查看:98
本文介绍了CMake:如果任何可执行目标都需要,则构建目标库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目布局如下:

src/
    include/
           include1.h
           include2.h
           include3.h

    lib/
        lib1/
            source1_lib1.c
            source2_lib1.c
        lib2/
            source1_lib2.c
            source2_lib2.c
            source3_lib2.c
        lib3/
            source1_lib3.c
        lib4/
            source1_lib4.c
            source2_lib4.c

    module_A/ (this module will need lib1 and lib4)
            source1_moduleA.c
            source2_moduleA.c

    module_B/ (this module will need lib2 and lib3)
            source1_moduleB.c
            source2_moduleB.c
            source3_moduleB.c

    module_C/ (this module will need lib1, lib2, lib3 and lib4)
            source1_moduleC.c

    module_D/ (this module will need lib1 and lib3)
            source1_moduleD.c
            source2_moduleD.c
            source3_moduleD.c
            source4_moduleD.c

全局解决方案可以由任意数量的module_X(取决于客户)

The global solution can be made by any number of module_X (it depends on the customer)

我的项目CMake文件位于 / src下包括一个配置文件(由客户需求定义)。此配置文件指示必须构建哪些模块并将其打包给目标客户。

My project CMake file located under "/src" includes a configuration file (it is defined by a customer needs). This configuration file indicates which modules must be built and packaged to the target customer.

假设我有一个客户X,他选择的模块是module_A和module_D。在这种情况下,我的构建系统应该只构建lib_1,lib_3和lib_4。

Let's say I have a customer X and the modules he selected are module_A and module_D. In this case my build system should only builds lib_1, lib_3 and lib_4.

我正在寻找一种定义目标库的方法,而无需构建它们,直到我引用它们为止

What I am looking for is a way to define target libraries without being built until I do reference them in one the CMakeLists files under module_X directories.

哦,我的错了,我错过了对您的帮助表示感谢

Oh my bad I missed to say A BIG THANKS FOR YOUR HELP

推荐答案

CMake无法立即执行此操作。将构建通过 add_library 调用添加的任何库。

CMake can not do this out of the box. Any library that is added through an add_library call will be built.

但是您可以在内部实现所需的行为CMake。您可以直接调用自定义包装函数,而不是直接调用 add_library add_executable add_library 包装程序仅存储调用 add_library 所需的信息,但不直接调用它。 add_executable 包装器迭代可执行文件的所有依赖项,并在需要时调用 add_library

But you can implement the behavior you want inside the CMake. Instead of calling add_library and add_executable directly you would call custom wrapper functions. The add_library wrapper simply stores the information required to call add_library, but does not call it directly. The add_executable wrapper iterates over all dependencies of the executable and makes the call to add_library if required.

您将必须设计自己的系统,以维护状态是否已添加特定库以及构建库目标的参数是什么。在尊重瞬时依赖关系时,事情也会变得更加困难(module_1依赖于lib_a,而lib_a依次依赖于lib_b;但是没有可执行文件直接依赖于lib_b)。但是完全有可能用几百行CMake代码构建这样的系统。

You will have to design your own system for maintaining the state whether a particular library has already been added and what the parameters are for constructing the library target. Things get slightly more difficult when respecting transient dependencies as well (module_1 depends on lib_a which in turn depends on lib_b; but no executable depends on lib_b directly). But it is perfectly possible to build such a system with a few hundred lines of CMake code.

这篇关于CMake:如果任何可执行目标都需要,则构建目标库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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