c++ - 生成库的多个实例的 Makefile

标签 c++ c makefile cmake

生成多个库文件实例的最佳方法是什么。 例如,考虑以下示例:

Lib.h (Inst1)        Lib.h (Inst2)
    ¦                     ¦
    ----------------------
              ¦
            Lib.c
              ¦
      ----------------
      ¦              ¦
   FolderA       FolderB
   (Lib.a)       (Lib.a)     -> Here are 2 different instances of the library

注意:两个版本的 Lib.a 名称相同但内容不同。 例如,当包含包含不同的#define 值时,可能会发生这种情况:

#define VAR1 0 -> Defined in Lib.h (Inst1)
#define VAR1 5 -> Defined in Lib.h (Inst2)

=> 几个版本的 Lib.a

我曾想过拥有一个包含所有可能的组合所需的主 makefile,但这很快就会变得难以管理。

这可以以结构化的方式完成吗?执行此类操作的典型方法是什么?

最佳答案

假设您要根据现有lib.h 的内容生成lib.a(该文件只有一个拷贝,但可以有不同的内容...),你可以这样做:

target_dir = $(shell some commands to figure out desired target dir from lib.h)

all: ${target_dir}/lib.a

%/lib.a: common/lib.h
     @echo doing some commands to build lib.a

这会根据 lib.h 的内容在正确的目录中构建 lib.a。

另一方面,如果您有多个 lib.h 拷贝,那么您想要的效果如下:

%/lib.a: %/lib.h
     @echo doing some commands to build lib.a from $^

最后,如果目录名称不对齐,您可以使用一系列规则来映射它:

FolderA/lib.a: Inst1/lib.h
FolderB/lib.a: Inst2/lib.h

%/lib.a:
    @echo doing some commands to build lib.a from $^

如果你想基于某个数组或类似的东西生成多个版本的 lib.h,那就是另一回事了......

关于c++ - 生成库的多个实例的 Makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47453391/

相关文章:

c - 解释 Unix 中程序的文字运行时间

c - Valgrind 错误 : conditional jump or move depends on uninitialised value(s) - C

c - Cygwin 中无法识别的命令行选项 ‘-pthread’

makefile - gnu make、microsoft nmake 和 posix standard make 有何相似/不同?

c++ - 在 std::vector 上存储带有模板的类实例

c++ - 如何在运行时将编辑样式更改为 ES_NUMBER?

c++ - Qt C++ QWebView图像质量

c++ - Qt 中的 PaintEvent 为类的每个实例绘制相同的所有小部件

c - 通过循环打印双数组

makefile - makefile 的当前工作目录