My linux c++ gnu makefile variable expansion does not behave as I expected it to












0















I have created the following little makefile snippet. Note: I have made this a minimal example of my problem so it is a pointless makefile.



TARGET = none
OBJ_BASE_DIR = obj

# Linux x86 c++ compiler
.PHONY: build_cpp_x86Linux
build_cpp_x86Linux: TARGET = x86Linux
build_cpp_x86Linux: build

OBJ_DIR = $(addsuffix /$(TARGET),$(OBJ_BASE_DIR))

$(info TARGET IS: $(TARGET))
$(info OBJ_DIR IS: $(OBJ_DIR))

build: $(OBJ_DIR)/test.o
@echo building, OBJ_DIR: $(OBJ_DIR)

# pattern rule
$(OBJ_DIR)/%.o:
@echo "compiling $@"


Here is the output of calling make:




TARGET IS: none
OBJ_DIR IS: obj/none
compiling obj/none/test.o
building, OBJ_DIR: obj/x86Linux



From the output you can see that it is trying to compile obj/none/test.o, but what I want it to do is try to compile obj/x86Linux/test.o. I am not quite sure what is going on here. I think I understand that the makefile expands the variables on the first pass (which would result in TARGET=none), but I thought that it would re-expand the variables again once I have called the target build_cpp_x86Linux which sets the value of TARGET to x86Linux...



What I am doing wrong here and how should this be done?










share|improve this question




















  • 1





    Note: .PHONEY --> .PHONY.

    – G.M.
    Nov 18 '18 at 20:03











  • @G.M. good spot.. fixed

    – code_fodder
    Nov 18 '18 at 20:11






  • 1





    It's possible to fix this with second-expansion, but you really shouldn't use targets for the job of a makefile. And if you do want to use targets, call another makefile recursively.

    – o11c
    Nov 18 '18 at 20:43











  • @o11c hmm.... how can this be solved with second-expansion?, I took a look at this but could not quite figure out how to use it for this purpose. Also re: calling makefile I don't get if you mean call the "makefile recursively" or "call another makefile".... I think I get how each of those could work (by passing variables into the second makefile call... : )

    – code_fodder
    Nov 18 '18 at 20:48











  • Is "none" even a real value, or is it just a dummy?

    – amn
    Nov 18 '18 at 21:00
















0















I have created the following little makefile snippet. Note: I have made this a minimal example of my problem so it is a pointless makefile.



TARGET = none
OBJ_BASE_DIR = obj

# Linux x86 c++ compiler
.PHONY: build_cpp_x86Linux
build_cpp_x86Linux: TARGET = x86Linux
build_cpp_x86Linux: build

OBJ_DIR = $(addsuffix /$(TARGET),$(OBJ_BASE_DIR))

$(info TARGET IS: $(TARGET))
$(info OBJ_DIR IS: $(OBJ_DIR))

build: $(OBJ_DIR)/test.o
@echo building, OBJ_DIR: $(OBJ_DIR)

# pattern rule
$(OBJ_DIR)/%.o:
@echo "compiling $@"


Here is the output of calling make:




TARGET IS: none
OBJ_DIR IS: obj/none
compiling obj/none/test.o
building, OBJ_DIR: obj/x86Linux



From the output you can see that it is trying to compile obj/none/test.o, but what I want it to do is try to compile obj/x86Linux/test.o. I am not quite sure what is going on here. I think I understand that the makefile expands the variables on the first pass (which would result in TARGET=none), but I thought that it would re-expand the variables again once I have called the target build_cpp_x86Linux which sets the value of TARGET to x86Linux...



What I am doing wrong here and how should this be done?










share|improve this question




















  • 1





    Note: .PHONEY --> .PHONY.

    – G.M.
    Nov 18 '18 at 20:03











  • @G.M. good spot.. fixed

    – code_fodder
    Nov 18 '18 at 20:11






  • 1





    It's possible to fix this with second-expansion, but you really shouldn't use targets for the job of a makefile. And if you do want to use targets, call another makefile recursively.

    – o11c
    Nov 18 '18 at 20:43











  • @o11c hmm.... how can this be solved with second-expansion?, I took a look at this but could not quite figure out how to use it for this purpose. Also re: calling makefile I don't get if you mean call the "makefile recursively" or "call another makefile".... I think I get how each of those could work (by passing variables into the second makefile call... : )

    – code_fodder
    Nov 18 '18 at 20:48











  • Is "none" even a real value, or is it just a dummy?

    – amn
    Nov 18 '18 at 21:00














0












0








0








I have created the following little makefile snippet. Note: I have made this a minimal example of my problem so it is a pointless makefile.



TARGET = none
OBJ_BASE_DIR = obj

# Linux x86 c++ compiler
.PHONY: build_cpp_x86Linux
build_cpp_x86Linux: TARGET = x86Linux
build_cpp_x86Linux: build

OBJ_DIR = $(addsuffix /$(TARGET),$(OBJ_BASE_DIR))

$(info TARGET IS: $(TARGET))
$(info OBJ_DIR IS: $(OBJ_DIR))

build: $(OBJ_DIR)/test.o
@echo building, OBJ_DIR: $(OBJ_DIR)

# pattern rule
$(OBJ_DIR)/%.o:
@echo "compiling $@"


Here is the output of calling make:




TARGET IS: none
OBJ_DIR IS: obj/none
compiling obj/none/test.o
building, OBJ_DIR: obj/x86Linux



From the output you can see that it is trying to compile obj/none/test.o, but what I want it to do is try to compile obj/x86Linux/test.o. I am not quite sure what is going on here. I think I understand that the makefile expands the variables on the first pass (which would result in TARGET=none), but I thought that it would re-expand the variables again once I have called the target build_cpp_x86Linux which sets the value of TARGET to x86Linux...



What I am doing wrong here and how should this be done?










share|improve this question
















I have created the following little makefile snippet. Note: I have made this a minimal example of my problem so it is a pointless makefile.



TARGET = none
OBJ_BASE_DIR = obj

# Linux x86 c++ compiler
.PHONY: build_cpp_x86Linux
build_cpp_x86Linux: TARGET = x86Linux
build_cpp_x86Linux: build

OBJ_DIR = $(addsuffix /$(TARGET),$(OBJ_BASE_DIR))

$(info TARGET IS: $(TARGET))
$(info OBJ_DIR IS: $(OBJ_DIR))

build: $(OBJ_DIR)/test.o
@echo building, OBJ_DIR: $(OBJ_DIR)

# pattern rule
$(OBJ_DIR)/%.o:
@echo "compiling $@"


Here is the output of calling make:




TARGET IS: none
OBJ_DIR IS: obj/none
compiling obj/none/test.o
building, OBJ_DIR: obj/x86Linux



From the output you can see that it is trying to compile obj/none/test.o, but what I want it to do is try to compile obj/x86Linux/test.o. I am not quite sure what is going on here. I think I understand that the makefile expands the variables on the first pass (which would result in TARGET=none), but I thought that it would re-expand the variables again once I have called the target build_cpp_x86Linux which sets the value of TARGET to x86Linux...



What I am doing wrong here and how should this be done?







c++ linux makefile






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 20:11







code_fodder

















asked Nov 18 '18 at 19:51









code_foddercode_fodder

5,22463775




5,22463775








  • 1





    Note: .PHONEY --> .PHONY.

    – G.M.
    Nov 18 '18 at 20:03











  • @G.M. good spot.. fixed

    – code_fodder
    Nov 18 '18 at 20:11






  • 1





    It's possible to fix this with second-expansion, but you really shouldn't use targets for the job of a makefile. And if you do want to use targets, call another makefile recursively.

    – o11c
    Nov 18 '18 at 20:43











  • @o11c hmm.... how can this be solved with second-expansion?, I took a look at this but could not quite figure out how to use it for this purpose. Also re: calling makefile I don't get if you mean call the "makefile recursively" or "call another makefile".... I think I get how each of those could work (by passing variables into the second makefile call... : )

    – code_fodder
    Nov 18 '18 at 20:48











  • Is "none" even a real value, or is it just a dummy?

    – amn
    Nov 18 '18 at 21:00














  • 1





    Note: .PHONEY --> .PHONY.

    – G.M.
    Nov 18 '18 at 20:03











  • @G.M. good spot.. fixed

    – code_fodder
    Nov 18 '18 at 20:11






  • 1





    It's possible to fix this with second-expansion, but you really shouldn't use targets for the job of a makefile. And if you do want to use targets, call another makefile recursively.

    – o11c
    Nov 18 '18 at 20:43











  • @o11c hmm.... how can this be solved with second-expansion?, I took a look at this but could not quite figure out how to use it for this purpose. Also re: calling makefile I don't get if you mean call the "makefile recursively" or "call another makefile".... I think I get how each of those could work (by passing variables into the second makefile call... : )

    – code_fodder
    Nov 18 '18 at 20:48











  • Is "none" even a real value, or is it just a dummy?

    – amn
    Nov 18 '18 at 21:00








1




1





Note: .PHONEY --> .PHONY.

– G.M.
Nov 18 '18 at 20:03





Note: .PHONEY --> .PHONY.

– G.M.
Nov 18 '18 at 20:03













@G.M. good spot.. fixed

– code_fodder
Nov 18 '18 at 20:11





@G.M. good spot.. fixed

– code_fodder
Nov 18 '18 at 20:11




1




1





It's possible to fix this with second-expansion, but you really shouldn't use targets for the job of a makefile. And if you do want to use targets, call another makefile recursively.

– o11c
Nov 18 '18 at 20:43





It's possible to fix this with second-expansion, but you really shouldn't use targets for the job of a makefile. And if you do want to use targets, call another makefile recursively.

– o11c
Nov 18 '18 at 20:43













@o11c hmm.... how can this be solved with second-expansion?, I took a look at this but could not quite figure out how to use it for this purpose. Also re: calling makefile I don't get if you mean call the "makefile recursively" or "call another makefile".... I think I get how each of those could work (by passing variables into the second makefile call... : )

– code_fodder
Nov 18 '18 at 20:48





@o11c hmm.... how can this be solved with second-expansion?, I took a look at this but could not quite figure out how to use it for this purpose. Also re: calling makefile I don't get if you mean call the "makefile recursively" or "call another makefile".... I think I get how each of those could work (by passing variables into the second makefile call... : )

– code_fodder
Nov 18 '18 at 20:48













Is "none" even a real value, or is it just a dummy?

– amn
Nov 18 '18 at 21:00





Is "none" even a real value, or is it just a dummy?

– amn
Nov 18 '18 at 21:00












1 Answer
1






active

oldest

votes


















1














You could also use:



TARGET?=none


And then override on the command line TARGET=x86Linux



You can also use ifdef or other scanning if operations to set different variables based on these arguments or environment variables.






share|improve this answer
























  • Similar comment to MadScientists comment. Upvoted since it does work, but not quite right yet for me. But I think from what I have read so far a good approach for me is to have another layer of makefile to set these target variables and then call make again on another makefile or some such....

    – code_fodder
    Nov 18 '18 at 22:59











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364833%2fmy-linux-c-gnu-makefile-variable-expansion-does-not-behave-as-i-expected-it-to%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









1














You could also use:



TARGET?=none


And then override on the command line TARGET=x86Linux



You can also use ifdef or other scanning if operations to set different variables based on these arguments or environment variables.






share|improve this answer
























  • Similar comment to MadScientists comment. Upvoted since it does work, but not quite right yet for me. But I think from what I have read so far a good approach for me is to have another layer of makefile to set these target variables and then call make again on another makefile or some such....

    – code_fodder
    Nov 18 '18 at 22:59
















1














You could also use:



TARGET?=none


And then override on the command line TARGET=x86Linux



You can also use ifdef or other scanning if operations to set different variables based on these arguments or environment variables.






share|improve this answer
























  • Similar comment to MadScientists comment. Upvoted since it does work, but not quite right yet for me. But I think from what I have read so far a good approach for me is to have another layer of makefile to set these target variables and then call make again on another makefile or some such....

    – code_fodder
    Nov 18 '18 at 22:59














1












1








1







You could also use:



TARGET?=none


And then override on the command line TARGET=x86Linux



You can also use ifdef or other scanning if operations to set different variables based on these arguments or environment variables.






share|improve this answer













You could also use:



TARGET?=none


And then override on the command line TARGET=x86Linux



You can also use ifdef or other scanning if operations to set different variables based on these arguments or environment variables.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 20:35









Matthieu BrucherMatthieu Brucher

15.6k32140




15.6k32140













  • Similar comment to MadScientists comment. Upvoted since it does work, but not quite right yet for me. But I think from what I have read so far a good approach for me is to have another layer of makefile to set these target variables and then call make again on another makefile or some such....

    – code_fodder
    Nov 18 '18 at 22:59



















  • Similar comment to MadScientists comment. Upvoted since it does work, but not quite right yet for me. But I think from what I have read so far a good approach for me is to have another layer of makefile to set these target variables and then call make again on another makefile or some such....

    – code_fodder
    Nov 18 '18 at 22:59

















Similar comment to MadScientists comment. Upvoted since it does work, but not quite right yet for me. But I think from what I have read so far a good approach for me is to have another layer of makefile to set these target variables and then call make again on another makefile or some such....

– code_fodder
Nov 18 '18 at 22:59





Similar comment to MadScientists comment. Upvoted since it does work, but not quite right yet for me. But I think from what I have read so far a good approach for me is to have another layer of makefile to set these target variables and then call make again on another makefile or some such....

– code_fodder
Nov 18 '18 at 22:59




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364833%2fmy-linux-c-gnu-makefile-variable-expansion-does-not-behave-as-i-expected-it-to%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini