user1479670
user1479670

Reputation: 1281

How to write make rules for a series of similar compiles?

In my project i need to compile a series of c++ files into so-files.

For each file i have to do this:

XXXWrapper.o: XXXWrapper.cpp XXXWrapper.h
    $(CC) $(COPT) $(INCL) -fPIC

XXXWrapper.so:XXXWrapper.o
$(CC) -shared  -o XXX.so XXXWrapper.o \
    $(POPS)/XXX.o -lhdf5 \
    -L$(KERNEL) -lKernel \
    -L$(COMMON) -lCommon 

Here $(INCL) contains all necessary includes, and

COPT=-c $< -I $(COMMON) -o $@ $(CADD) $(CFLAGS)

In order to avoid repeating the same rules 40 times with only small variations, i tried this:

%Wrapper.o: %Wrapper.cpp %Wrapper.h  
    $(CC) $(COPT) $(INCL) -fPIC

%Wrapper.so: %Wrapper.o
    $(CC) -shared  -o $(subst,"Wrapper","",$@).so  $< \
    $(POPS)/$(subst,"Wrapper","",$@) -lhdf5 \
    -L$(KERNEL) -lKernel \
    -L$(COMMON) -lCommon 

hoping that $(subst,"Wrapper","",$@) would return the part of the input filename before "Wrapper". While the first rule worked well, result of the substitution was empty in the second rule:

~/utils/cgcc -c XXXWrapper.cpp -I ../common -o XXXWrapper.o -g -Wall  
-I../common -I../kernel -I../icosa -I../populations -I../genes -I../io -I../modular" -fPIC

~/utils/cgcc -shared  -o .so  XXXWrapper.o \
    ../populations/.o -lhdf5 \
-L../kernel -lKernel \
-L../common -lCommon 

(note the -o .so and the ../populations/.o) Even if this had worked, the outputfile would have been named XXX.o.so, but i could have lived with this.

So I guess this means the substitution command can't be used like this.

Is there a way to achieve what i have in mind?

Upvotes: 0

Views: 20

Answers (1)

MadScientist
MadScientist

Reputation: 100956

First, this is wrong:

$(subst,"Wrapper","",$@)

Besides the fact that there should be no comma after subst, quotes are not special to make; this is substituting the literal string "Wrapper" with the literal string "" (including quotes). Since there is no string "Wrapper" in your target, this does nothing.

Second, it sounds to me like you just want $*.so which would be a lot easier.

Lastly, it's almost always wrong to create an output file that is different than the one you told make you would create. I can only assume you have some obscure and compelling reason to want to do this...

Upvotes: 1

Related Questions