Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU Make: Using % in a pattern-rule macro

I'm trying to build a pattern-rule that allows me to use % in a macro call and I'm not getting the results I expect.

The pattern-rule:

%.o: %.cpp $(%_H)
    g++ -o $@ $(FLAGS) -c $<

The problem is with the $(%_H)

For some reason % isn't expanding to what it is defined to.

When I print out the dependency list ($^), only the source file prints.

I have a very clean way of setting up my dependencies that I would like to use:

# Header Dependencies per object 
Geometry_H:=Geometry.h $(Error_H) 
Enemy_H:=Enemy.h $(Geometry_H) $(Error_H) 
Surface_H:=Surface.h $(Geometry_H) 
Player_H:=Player.h $(Geometry_H) $(Surface_H) 
SDLWindow_H:=SDLWindow.h $(Surface_H) $(Error_H) 
Path_H:=Path.h $(Geometry_H) $(Error_H) 
Territory_H:=Territory.h $(Geometry_H) 
Board_H:=Board.h $(Territory_H) $(Geometry_H) $(Player_H) $(Path_H) $(Enemy_H) $(Error_H) 
Error_H:=Error.h 
Diminisher_H:=Diminisher.h $(SDLWindow_H) $(Geometry_H) $(Surface_H) $(Board_H) $(Error_H) 
Main_H:=$(Diminisher_H)

Another person has suggested that I change these variables into dependency lists.

I.e. Main_H:=$(Diminisher_H)

becomes Main.o: $(Diminisher_H)

This is a good fix, it works. However, the problem still remains that $(%_H) is somehow invalid.

I would like to know how (if possible) to make it a valid expression.

I've tried $( $%_H ), $( $(%)_H ), $( $(value %)_H ) and many more. It seems like % just loses its meaning when in a macro call.

Is there no way to use % in a macro call?

like image 844
joshterrell805 Avatar asked Oct 07 '22 16:10

joshterrell805


1 Answers

Either use secondary expansion:

.SECONDEXPANSION:

%.o: %.cpp $$($$*_H)
    g++ -o $@ $(FLAGS) -c $<

Or (IMO, better) use dependency auto-generation:

  • An answer to another SO question
  • Advanced Auto-Dependency Generation article
like image 121
Eldar Abusalimov Avatar answered Oct 13 '22 11:10

Eldar Abusalimov