progLearner
progLearner

Reputation: 123

Makefile generating multiple instances of a library

What is the best way to generate multiple instances of library files. For instance, considering the following example:

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

NB: Both versions of Lib.a will have the same name but different contents. This could happen for instance when includes contain different #define values:

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

=> several versions of Lib.a

I thought about having a main makefile containing all the possible combination needed, but that will quickly become unmanageable.

Can this be done in a structured way? What is the typical approach to do something like that?

Upvotes: 0

Views: 175

Answers (3)

HardcoreHenry
HardcoreHenry

Reputation: 6387

Assuming that you want to generate lib.a based on the contents of an existing lib.h (there's one copy of the file, but it can have different contents...), you could do something like:

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

Which would build lib.a in the correct directory based on the contents of the lib.h.

If, on the other hand, you have multiple copies of lib.h, then you want something to the effect of:

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

Finally, if the directory names don't line up, you could map it using a bunch of rules:

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

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

If you want to generate multiple versions of lib.h based on some array or something like that, then that's another story...

Upvotes: 2

Mathieu
Mathieu

Reputation: 9699

You can pass variable at make invocation.

Imagine your makefile is:

all: $(DIR)/lib.so

$(DIR)/lib.so: lib.c
    $(CC) -shared -I$(DIR) -o $@ $<

You can call it with: make DIR=Inst1

So you can generate all your library in one call:

#! /bin/sh
for I in Inst*; do
    make DIR=$I
done

Upvotes: 1

pptaszni
pptaszni

Reputation: 8345

Considering your comment, maybe you can do like this:

  1. Define the rules for compiling libVar1Eq0.a with VAR1 == 0
  2. Define the rules for compiling libVar1Eq5.a with VAR1 == 5
  3. Define the rule for target FolderA/lib.a as ln -s libVar1Eq0.a FolderA/lib.a
  4. Define the rule for target FolderB/lib.a as ln -s libVar1Eq5.a FolderB/lib.a

If you have a lot of different lib.h, it can easily be automated.

Upvotes: 1

Related Questions