man make: a Primer on the Make Utility
Predefined Variables
-
$?— evaluates to the list of components that are younger than the current target. Can be used only in description file command lines. -
$@— evaluates to the current target name. Can be used only in description file command lines. -
$$@— also evaluates to the current target name. However, it can be used only on dependency lines. -
$<— the name of the related file that caused the action (the precursor to the target). This is only for suffix rules. -
$*— the shared prefix of the target and dependent—only for suffix rules.
Common Variables for C++ Programming
-
CC— the name of the compiler. -
DEBUG— the debugging flag. This is -g in both g++ and cxx. The purpose of the flag is to include debugging information into the executable, so that you can use utilities like gdb to debug the code. -
LFLAGS— the flags used in linking. As it turns out, you don't need any special flags for linking. The option listed is-Wall, which tells the compiler to print all warnings. But, that's fine. We can use that. -
CFLAGS— the flags used in compiling and creating object files. This includes both-Walland-c. The-coption is needed to create object files—that is, .o files.
Suffix Rules
In certain situations, you will find that the rules for a certain file type are identical except for the filename. For instance, a lot of times in a C project, you will see rules like this:
file.o: file.c
cc -O -Wall file.c
because for every .c file, you need to make the intermediate .o file, so that the end binary then can be built. Suffix rules are a way of minimizing the amount of time you spend writing out rules and the number of rules in your makefile. In order to use suffix rules, you need to tell make which file suffixes are considered significant (suffix rules won't work unless the suffix is defined this way), then write the generic rule for the suffixes. In the case described above, you would do this:
<![CDATA[
.SUFFIXES: .o .c
.c.o:
cc -O -Wall $<
]]>
You may note that in the case of suffix rules, the dependency suffix goes
before the target suffix, which is a reversal from the normal order in a
makefile. You also will see that you use $< in the command, which evaluates
to the .c filename associated with the .o file that triggered the rule.
There are a couple predefined variables like this that are used exclusively
for suffix rules:
-
$<— evaluates to the component that is being used to make the target—that is, file.c. -
$*— evaluates to the filename part (without any suffix) of the component that is being used to make the target—that is, file.
Note that the $? variable cannot occur in suffix
rules, but the $@ variable
still will work.
Command Special Characters
Certain characters can be used in conjunction with commands to
alter the behavior of make or the command. If you're familiar with
shell scripting, you'll recognize that \ signifies a line continuation.
That is to say, using \ means that the command isn't finished and continues
on the next line. Nobody likes looking at a messy file, and using this
character at the end of a line helps keep your makefile clean and
pretty. If a rule has more than one command, use a semicolon to separate
commands. You can start a command with a hyphen, and make will ignore any
errors that occur from the command. If you want to suppress the output of a
command during execution, start the command with an at sign (@).
Using these symbols will allow you to make a more usable and readable makefile.
Directives
Sometimes, you need more control over how the makefile is read and executed. Directives are designed exactly for that purpose.
From defining, overriding or exporting variables to importing other makefiles, these directives are what make a more robust makefile possible. The most useful of the directives are the conditional directives though.
Conditional directives allow you to define multiple versions of a command based on preexisting conditions. For example, say you have a set of libraries you want included in your binary only if the compiler used is gcc:
libs_for_gcc = -lgnu
normal_libs =
foo: $(objects)
ifeq ($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
else
$(CC) -o foo $(objects) $(normal_libs)
endif
In this example, you use ifeq to check if CC equals
gcc and if it
does, use the gcc libraries; otherwise, use the generic libraries.
This is just a small, basic sampling of the things you can do with make and makefiles. There are so many more complex and interesting things you can do, you just have to dig around to find them!
Resources
GNU make comes with most Linux distributions by default, but it can be found on the main GNU FTP server: http://ftp.gnu.org/gnu/make (via HTTP) and ftp://ftp.gnu.org/gnu/make (via FTP). It also can be found on the GNU mirrors at http://www.gnu.org/prep/ftp.html.
Documentation for make is available on-line at
http://www.gnu.org/software/make/manual, as is documentation for most GNU
software. You also can find more information about make by running
info make
or man make, or by looking at /usr/doc/make/, /usr/local/doc/make/ or
similar directories on your system. A brief summary is available by running
make --help.
- « first
- ‹ previous
- 1
- 2
- 3
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- Non-Linux FOSS: libnotify, OS X Style
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- RSS Feeds
- Reply to comment | Linux Journal
29 min 33 sec ago - Reply to comment | Linux Journal
2 hours 55 min ago - Reply to comment | Linux Journal
6 hours 55 min ago - Yeah, user namespaces are
8 hours 11 min ago - Cari Uang
11 hours 42 min ago - user namespaces
14 hours 36 min ago - yea
15 hours 2 min ago - One advantage with VMs
17 hours 30 min ago - about info
18 hours 3 min ago - info
18 hours 4 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Thanks a lot for the intro
Just a question:
For the last line in page 2:
will produce the output abcdef xyzabc def.
Why not:
will produce the output abcdef defabc def?
Make is an old system that
Make is an old system that should only be used by complex build system maintainers that need the extra power and automatic tools (and by complex build systems I mean the Linux Kernel -- which uses some autobuild system itself (KBuild), that is built on top of a specialized make system).
To compile programs you should instead use autotools - at least automake and for ease of use I highly recommend autoconf.
Great intro to the topic - 2
Great intro to the topic - 2 things that I noticed as I was following the tutorial:
In Listing 1:
all: $(SOURCES) $(EXECUTABLE)
can be replaced by
all: $(EXECUTABLE)
since, according to your description about suffix rules, $(EXECUTABLE) depends on .o files, which will be rebuilt automatically on changes to .c files
Also, the .SUFFIXES fake target is missing from the listing:
.SUFFIXES: .o .c