What do these Makefile constructs mean?

Can someone help me figure out the following makefile?

BINS=file1 file2 file3 all: $(BINS) clean: rm -f $(BINS) *~ $*: $@.c gcc -g -o $@ $? 

Here are my questions:

  • What is the -g gcc option?
  • What is $ * and $@
  • How do you know how to accomplish the last goal?

Thanks!

+4
source share
5 answers

From the gcc and make documentation:

  • "- g Produce debugging information in the native format of the operating system."

  • a. "$ * The stem with which the implicit rule is associated (see" How Pattern Matching "). If the target is dir / a.foo.b and the target pattern is.%. B, then the stem is dir / foo. Useful for creating linked file names. In the static rule of the template, the bar is part of the file name that matches "%" in the target template. "

    b. "$ @ The name of the rule’s target file. If the target is a member of the archive," $@ "is the name of the archive file. In a template rule that has several goals (see Introduction to Template Rules) '$ @' is the name of what purpose was invoked to run the rule commands.

    with. "$? Names of all preconditions that are newer than the target, with spaces between them." (Not asked, but worth adding.)

  • "'all' Compile the entire program. This should be the default target.

This makefile example is a bit limited, as it seems to only create C programs. GNU make has some more extensive examples that are useful for learning how to write make files.

+5
source

1.) What is the -g option for gcc?

Generate Debug Information

2.) What is $ * and $@

$* is the basis of the rule. When you make a rule like

 %.ext: %.otherext ext2otherext --input $? --output $@ --logfile $*.log 

and the rule tries to make file.ext from file.otherext , then in log goes to file.log

$@ is the name of the target file ( file.ext in the case above).

3.) How do I know how to accomplish the last goal?

It's a secret.

$* used for intermediate files: for example, when you try to make %.so from %.c , you need the %.o intermediary, which is neither the target nor the source. You put:

 %.so: %.c gcc -fPIC -g -c -o $*.o $? ld -shared -o $@ $*.o 

In other words, the rule %.extension $* has everything except .extension .

As a goal, $* does not make sense.

Are you sure this rule is actually being implemented somewhere? When I try to play it using the Makefile, it applies the default rules ( cc -o ) instead of gcc -g -o

The rule you need looks like:

 %: %.c gcc -g -o $@ $? 
+2
source

The first line does what you expect by looking at it - it creates a list of names and assigns it a BIN . Then a make target is defined called all , which depends on the goals listed in $BIN ( all cannot be built until every target in $BIN ). all - special purpose of the assembly; if you just run make and not, say, make clean , the all target will be automatically built. There are no actual commands associated with all - it just guarantees that all of its dependencies will be built. The last rather simple command defines an assembly target called clean , which deletes every file specified in $BIN (note that $BIN used both as a list of build targets and a list of files in this make file) and all backups.

Next, we get into a line, which is mainly magic. $* is a special purpose of the assembly, which means "everything that is not specifically defined." When make tries to build all , he finds the target and starts working on its dependencies. But when he tries to build file1 , a clear goal is not defined. Instead, it uses $* to build file1 . Similarly, $@ is replaced with the assembly destination name and $? with its dependencies (like you better not check the manual). When make proceeds to create file1 , the $* rule forces it to behave as if the following rule were defined:

 file1: file1.c gcc -g -o file1 file1.c 

Finally, the -g option simply allows you to compile debugging information.

+1
source

Q1 and Q2 have already received the answers, so a brief explanation is only for Q3:

Always fulfill the first goal, unless you specify another. This is also called the default target. Typically, the default goal is called "all."

0
source

All Articles