[NTLUG:Discuss] Need help writing Makefile < Warning: Long Post >

rp8034l rp8034l at flash.net
Sat Nov 9 13:33:59 PST 2019


Greetings:

I'm trying to write a high quality Makefile. One that I can re-use as a
template for many future software projects. I'm having a problem - I
don't know if I am writing it wrong, don't understand the doc, or have
found a bug. I'm looking for hints or suggestions...

My original goal is to compile "C" source files into an executable
on/for Linux. The step I am having trouble with is where I generate the
"executable" file and "map" file. Example:

    gcc -Wl,-Map=example.map,--cref -o example example.o

Normal make rules would re-run the recipe for each target file. This
example would run gcc twice; generating both files and then regenerating
them again:

    example example.map: example.o
    <tab> gcc -Wl,-Map=${basename $@}.map,--cref -o ${basename $@} $^

There is something called a pattern rule that can run the recipe once to
generate both output files. Requires two "stanzas" - recipe info and
dependency info:

    % %.map: %.o
    <tab> gcc -Wl,-Map=${basename $@}.map,--cref -o ${basename $@} $^

    example example.map: example.o

Testing: Initial make run, no "example" or "example.map" files.
Everything works. It created both "example" & "example.map" Delete
"example" and re-run make. Created "example" and replaced "example.map"
to match the new "example". Good - expected/desired outcome. Delete
"example.map" and re-run make. Make reports:

    make: Nothing to be done for 'all'

Make stubbornly refuses to create "example.map"...

I went through multiple permutations to try and find the problem.  When
I re-wrote the Makefile replacing the executable "example" with
"example.elf" and updating rules accordingly - THE DARN THING WORKED...

Could it be that any pattern rule with "%" as a target is treated
special and/or differently? If so, how do I work around this?

Although a crude way to transfer a file, I've attached a Makefile I've
been using to test this process. If you use it, remember that each line
of the recipes must be indented with a <tab>; not spaces.

Again thanks for any advice/info in advance.

Richard Philley

--------------------------------------------------

.PHONY: all
all:	example.o example example.map

.PHONY: clean
clean:
	@echo "# clean"
	-rm -f -- example example.map

####################
# Pattern rules

%.o:
	@echo "# create $@"
	touch $@

% %.map: %.o
	@echo "# gcc/link target($@) prereqs($^)"
	touch ${basename ${@}}
	touch ${basename ${@}}.map

####################
# Dependencies

example example.map: example.o



More information about the Discuss mailing list