LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-02-2012, 08:38 AM   #1
SexyBachelor
LQ Newbie
 
Registered: May 2012
Posts: 4

Rep: Reputation: Disabled
GNU Make: Using % in a pattern-rule macro


Hi,

I'm revising my code to make it a bit cleaner and right now I'm working on the makefile.

I'm having a problem with defining a pattern rule that uses % to access another variable.
Code:
# program output name
OUT=a.out

# compiler flags
FLAGS=-ggdb -std=c++0x

# library dependencies
LIBS=-lSDL -lSDL_image -lboost_system -lboost_chrono -lGL

# where to store built objects
BIN=bin

# The objects to build
OBJECTS=Geometry.o Enemy.o Surface.o Player.o SDLWindow.o Path.o Territory.o Board.o Diminisher.o Main.o

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

# make program
default: $(BIN) $(addprefix $(BIN)/, $(OBJECTS))
    g++ -o $(OUT) $(FLAGS) $(addprefix $(BIN)/, $(OBJECTS)) $(LIBS)

# create object bin directory
$(BIN):
    @ if [ ! -d "./$(BIN)" ]; then \
        mkdir $(BIN); \
    fi
    @echo "mkdir $(BIN)"

# command to make an object
$(BIN)/%.o: %.cpp $(%_H)
    g++ -o $@ $(FLAGS) -c $<

# delete all objects and program
clean:
    rm -rf $(BIN) 
    rm -f $(OUT)
Previously I had all the make rules explicitly written out. For example, Geometry read:

Code:
$(BIN)/Geometry.o: Geometry.cpp $(Geometry_H)
    g++ -o $@ $(FLAGS) -c $<
The problem I'm having is in bold.

In the new code, $(%_H) isn't expanding to Geometry_H.
I've tried many other variants such as:
$($%_H)
$( $(value %)_H )

I'm hoping someone knows the correct syntax.

The project will still build with this code, but if I edit a header file and run make without cleaning first, make will not rebuild the objects that depend on this header as defined by the *_H variables.

The old code would rebuild the targets that depended on the edited header.
 
Old 06-02-2012, 08:53 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the solution could be the target dependent macro, but it will not simplify:
Code:
$(BIN)/targetfile.o := DEP = $(target_H)
...

$(BIN)/%.o: %.cpp $(DEP) $(BIN)
    g++ -o $@ $(FLAGS) -c $<
and do not foget to add the dependency for $(BIN)

Last edited by pan64; 06-02-2012 at 09:00 AM.
 
Old 06-02-2012, 01:51 PM   #3
SexyBachelor
LQ Newbie
 
Registered: May 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
the solution could be the target dependent macro, but it will not simplify:
Code:
$(BIN)/targetfile.o := DEP = $(target_H)
...

$(BIN)/%.o: %.cpp $(DEP) $(BIN)
    g++ -o $@ $(FLAGS) -c $<
and do not foget to add the dependency for $(BIN)
I'm confused by your code (RED).
why is there a variable called "bin/targetfile.o" being assigned to it's header definition (target_h)?

Then bin/targetfile.o, DEP, and target_h would all hold the same information.

But not only that, it still doesn't solve the issue of the % not resolving inside of the variable call in the dependecy list $(%_H).

With the code in red, being that DEP is defined to only one of the header variables, you would be saying that all the objects depend on that one header. Each object (Enemy.o, Player.o, Path.o) has it's own header variable (Enemy_H, Player_H, Path_H) and I'm just trying to figure a way for % which holds the current object's stem (Enemy, Player, Path) to turn into it's corrisponding header variable (Enemy_H,...).

Unless you're suggesting that I define DEP multiple times for every Object... but I might as well just leave the code how it used to be with each object's name typed explicitly (Enemy.o: Enemy.cpp $(Enemey_H)).

If I'm not understanding what you said correctly, please reiterate.
 
Old 06-03-2012, 04:33 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
sorry, I was not really clear I think, so here is a link: Computed Variable Names
Originally I was thinking about target specific macros, but probably the computed variables are the better choice.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
man/Makefile.am:11: `%'-style pattern rules are a GNU make extension aibutter Linux - Networking 3 06-24-2011 07:03 AM
GNU Make and GNU GCC DEBUG vs RELEASE compiler options aryan1 Programming 1 01-12-2010 12:29 PM
No rule to make target `powerpc-linux-gnu-gcc', needed by 'xxx.plugin.so' shaiva Linux - Newbie 4 11-22-2009 12:13 AM
Macro programs for Gnu. Baldorg Linux - Software 3 08-06-2004 09:31 PM
How to make rule for make install and make uninstall melinda_sayang Programming 1 06-14-2004 05:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration