Makefile Syntax unclear
This is my first Makefile, and I am can't figure out some of the syntax used. The questions are marked below.
$(BUILD_DIR)/%.o: %.c $(BUILD_DIR)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
- What is the usage of "$(BUILD_DIR)" in the dependency?
- What is the meaning of "$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@" in the role?
makefile
add a comment |
This is my first Makefile, and I am can't figure out some of the syntax used. The questions are marked below.
$(BUILD_DIR)/%.o: %.c $(BUILD_DIR)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
- What is the usage of "$(BUILD_DIR)" in the dependency?
- What is the meaning of "$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@" in the role?
makefile
add a comment |
This is my first Makefile, and I am can't figure out some of the syntax used. The questions are marked below.
$(BUILD_DIR)/%.o: %.c $(BUILD_DIR)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
- What is the usage of "$(BUILD_DIR)" in the dependency?
- What is the meaning of "$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@" in the role?
makefile
This is my first Makefile, and I am can't figure out some of the syntax used. The questions are marked below.
$(BUILD_DIR)/%.o: %.c $(BUILD_DIR)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
- What is the usage of "$(BUILD_DIR)" in the dependency?
- What is the meaning of "$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@" in the role?
makefile
makefile
asked Nov 21 '18 at 8:31
YaronYaron
62
62
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build
and that one of the source files is bar/foo.c
.
$(BUILD_DIR)
in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:
$(BUILD_DIR):
mkdir -p $@
But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
that tells make to consider only the existence of
$(BUILD_DIR)
, not its last modification time, when deciding to re-build or not.
$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
is just a combination of make automatic variables and functions.
$<
and$@
expand as the first prerequisite (bar/foo.c
) and the target (build/bar/foo.o
) respectively.
$(<:.c=.lst)
replaces.c
by.lst
in$<
:bar/foo.lst
.
$(notdir $(<:.c=.lst))
removes the directory part:foo.lst
.
All in all, for a bar/foo.c
source file, and with BUILD_DIR = build
, the pattern rule would be equivalent to:
build/bar/foo.o: bar/foo.c | build
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o
Note that there are two different situations to consider:
All your source files are in the same directory as the Makefile (no
bar/foo.c
, justfoo.c
). Then you can simplify your recipe:
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@
because the
$(notdir...)
is useless.
Your source files can be in sub-directories (
bar/foo.c
). Then you need the$(notdir...)
in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c
andbaz/foo.c
) you will have a name conflict for$(BUILD_DIR)/foo.lst
and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent tobuild/bar
(orbuild/baz
), not justbuild
. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:
$(BUILD_DIR)/%.o: %.c
mkdir -p $(dir $@)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407961%2fmakefile-syntax-unclear%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build
and that one of the source files is bar/foo.c
.
$(BUILD_DIR)
in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:
$(BUILD_DIR):
mkdir -p $@
But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
that tells make to consider only the existence of
$(BUILD_DIR)
, not its last modification time, when deciding to re-build or not.
$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
is just a combination of make automatic variables and functions.
$<
and$@
expand as the first prerequisite (bar/foo.c
) and the target (build/bar/foo.o
) respectively.
$(<:.c=.lst)
replaces.c
by.lst
in$<
:bar/foo.lst
.
$(notdir $(<:.c=.lst))
removes the directory part:foo.lst
.
All in all, for a bar/foo.c
source file, and with BUILD_DIR = build
, the pattern rule would be equivalent to:
build/bar/foo.o: bar/foo.c | build
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o
Note that there are two different situations to consider:
All your source files are in the same directory as the Makefile (no
bar/foo.c
, justfoo.c
). Then you can simplify your recipe:
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@
because the
$(notdir...)
is useless.
Your source files can be in sub-directories (
bar/foo.c
). Then you need the$(notdir...)
in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c
andbaz/foo.c
) you will have a name conflict for$(BUILD_DIR)/foo.lst
and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent tobuild/bar
(orbuild/baz
), not justbuild
. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:
$(BUILD_DIR)/%.o: %.c
mkdir -p $(dir $@)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.
add a comment |
As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build
and that one of the source files is bar/foo.c
.
$(BUILD_DIR)
in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:
$(BUILD_DIR):
mkdir -p $@
But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
that tells make to consider only the existence of
$(BUILD_DIR)
, not its last modification time, when deciding to re-build or not.
$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
is just a combination of make automatic variables and functions.
$<
and$@
expand as the first prerequisite (bar/foo.c
) and the target (build/bar/foo.o
) respectively.
$(<:.c=.lst)
replaces.c
by.lst
in$<
:bar/foo.lst
.
$(notdir $(<:.c=.lst))
removes the directory part:foo.lst
.
All in all, for a bar/foo.c
source file, and with BUILD_DIR = build
, the pattern rule would be equivalent to:
build/bar/foo.o: bar/foo.c | build
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o
Note that there are two different situations to consider:
All your source files are in the same directory as the Makefile (no
bar/foo.c
, justfoo.c
). Then you can simplify your recipe:
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@
because the
$(notdir...)
is useless.
Your source files can be in sub-directories (
bar/foo.c
). Then you need the$(notdir...)
in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c
andbaz/foo.c
) you will have a name conflict for$(BUILD_DIR)/foo.lst
and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent tobuild/bar
(orbuild/baz
), not justbuild
. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:
$(BUILD_DIR)/%.o: %.c
mkdir -p $(dir $@)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.
add a comment |
As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build
and that one of the source files is bar/foo.c
.
$(BUILD_DIR)
in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:
$(BUILD_DIR):
mkdir -p $@
But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
that tells make to consider only the existence of
$(BUILD_DIR)
, not its last modification time, when deciding to re-build or not.
$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
is just a combination of make automatic variables and functions.
$<
and$@
expand as the first prerequisite (bar/foo.c
) and the target (build/bar/foo.o
) respectively.
$(<:.c=.lst)
replaces.c
by.lst
in$<
:bar/foo.lst
.
$(notdir $(<:.c=.lst))
removes the directory part:foo.lst
.
All in all, for a bar/foo.c
source file, and with BUILD_DIR = build
, the pattern rule would be equivalent to:
build/bar/foo.o: bar/foo.c | build
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o
Note that there are two different situations to consider:
All your source files are in the same directory as the Makefile (no
bar/foo.c
, justfoo.c
). Then you can simplify your recipe:
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@
because the
$(notdir...)
is useless.
Your source files can be in sub-directories (
bar/foo.c
). Then you need the$(notdir...)
in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c
andbaz/foo.c
) you will have a name conflict for$(BUILD_DIR)/foo.lst
and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent tobuild/bar
(orbuild/baz
), not justbuild
. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:
$(BUILD_DIR)/%.o: %.c
mkdir -p $(dir $@)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.
As with most computer languages the syntax of make cannot be clear if you don't know it. If you are using GNU make the GNU make manual is your friend. In the following explanations I will assume that BUILD_DIR = build
and that one of the source files is bar/foo.c
.
$(BUILD_DIR)
in the list of prerequisites (dependencies) tells make that the build directory (in which object files are supposed to go) must exist before the recipe is executed; logical. There must be another rule somewhere to create the directory if it does not exist yet. Something like:
$(BUILD_DIR):
mkdir -p $@
But unless you forgot to copy an important character, this dependency is terribly sub-optimal. As the last modification time of a directory changes each time its content changes (files or sub-directories added or removed), it will force the re-compilation of all source files every time the directory changes, which is not what you want. A better dependency would be order-only:
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
that tells make to consider only the existence of
$(BUILD_DIR)
, not its last modification time, when deciding to re-build or not.
$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
is just a combination of make automatic variables and functions.
$<
and$@
expand as the first prerequisite (bar/foo.c
) and the target (build/bar/foo.o
) respectively.
$(<:.c=.lst)
replaces.c
by.lst
in$<
:bar/foo.lst
.
$(notdir $(<:.c=.lst))
removes the directory part:foo.lst
.
All in all, for a bar/foo.c
source file, and with BUILD_DIR = build
, the pattern rule would be equivalent to:
build/bar/foo.o: bar/foo.c | build
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=build/foo.lst bar/foo.c -o build/bar/foo.o
Note that there are two different situations to consider:
All your source files are in the same directory as the Makefile (no
bar/foo.c
, justfoo.c
). Then you can simplify your recipe:
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@
because the
$(notdir...)
is useless.
Your source files can be in sub-directories (
bar/foo.c
). Then you need the$(notdir...)
in your recipe. But be warned that if you have two source files with the same base name (bar/foo.c
andbaz/foo.c
) you will have a name conflict for$(BUILD_DIR)/foo.lst
and your Makefile will not work as expected. Moreover, the order-only prerequisite of the rule should be equivalent tobuild/bar
(orbuild/baz
), not justbuild
. And there should be a rule to create it if needed. If it is your case I suggest to change your pattern rule for:
$(BUILD_DIR)/%.o: %.c
mkdir -p $(dir $@)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
There are other solutions (secondary expansion...) but there are a bit too complicated for this already too long answer.
edited Nov 21 '18 at 10:16
answered Nov 21 '18 at 9:36
Renaud PacaletRenaud Pacalet
9,42321730
9,42321730
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407961%2fmakefile-syntax-unclear%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown