Makefile: variables are not included from other makefile











up vote
0
down vote

favorite












I want to use a common.mk file for common variable definitions and include this file in other Makefiles but in the first example I've done it is not working so I think that I have misunderstood something.



This is the Makefile:



CC=gcc
CFLAGS=-g -Wall
LIB_FLAGS=-L/usr/local/lib/ -lcgreen

BUILDDIR=$(CURDIR)/build
SRC=$(wildcard *.c)

export BUILDDIR
export CFLAGS
export LIB_FLAGS

#include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined

unittests: dir externals $(OBJ)
@echo "SRC: $(SRC)"
@echo "OBJ: $(OBJ)" # THIS PRINTS OBJ AS EMPTY <------------------------------
$(CC) $(BUILDDIR)/*.o $(LIB_FLAGS) -o $(BUILDDIR)/unittests
$(BUILDDIR)/unittests

externals:
@$(MAKE) -C lib1 -f lib1.mk

dir:
mkdir -p $(BUILDDIR)


This is the common.mk file:



 OBJ=$(patsubst %.c, $(BUILDDIR)/%.o, $(notdir $(SRC)))

$(BUILDDIR)/%.o: %.c
@echo "File: "$<
$(CC) -c $(CFLAGS) $< -o $@

clean:
rm -f $(OBJ)


So I was expecting that the OBJ variable in the main Makefile had a object files list but it is empty and I don't understand why. Isn't it including the common.mk file the same as copying it's content into the Makefile?



Thanks










share|improve this question


















  • 1




    #include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined doesn't include anything. It starts with #, so it's just a comment.
    – melpomene
    Nov 19 at 19:13










  • @melpomene Thanks.. I am used to C and didn't notice it.. two hours lost because of that! so embarrasing.. Ouch!
    – jap jap
    Nov 19 at 19:16






  • 1




    If you use an editor which has some sort of makefile editing mode it will show comments in a different color. It will also do better at handling TAB vs. spaces, typically. For debugging it can be helpful to add $(info ...) functions into your makefiles to show variable values at various times etc. Adding these into common.mk would have probably let you know that it wasn't being parsed at all, pretty quickly.
    – MadScientist
    Nov 19 at 19:21















up vote
0
down vote

favorite












I want to use a common.mk file for common variable definitions and include this file in other Makefiles but in the first example I've done it is not working so I think that I have misunderstood something.



This is the Makefile:



CC=gcc
CFLAGS=-g -Wall
LIB_FLAGS=-L/usr/local/lib/ -lcgreen

BUILDDIR=$(CURDIR)/build
SRC=$(wildcard *.c)

export BUILDDIR
export CFLAGS
export LIB_FLAGS

#include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined

unittests: dir externals $(OBJ)
@echo "SRC: $(SRC)"
@echo "OBJ: $(OBJ)" # THIS PRINTS OBJ AS EMPTY <------------------------------
$(CC) $(BUILDDIR)/*.o $(LIB_FLAGS) -o $(BUILDDIR)/unittests
$(BUILDDIR)/unittests

externals:
@$(MAKE) -C lib1 -f lib1.mk

dir:
mkdir -p $(BUILDDIR)


This is the common.mk file:



 OBJ=$(patsubst %.c, $(BUILDDIR)/%.o, $(notdir $(SRC)))

$(BUILDDIR)/%.o: %.c
@echo "File: "$<
$(CC) -c $(CFLAGS) $< -o $@

clean:
rm -f $(OBJ)


So I was expecting that the OBJ variable in the main Makefile had a object files list but it is empty and I don't understand why. Isn't it including the common.mk file the same as copying it's content into the Makefile?



Thanks










share|improve this question


















  • 1




    #include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined doesn't include anything. It starts with #, so it's just a comment.
    – melpomene
    Nov 19 at 19:13










  • @melpomene Thanks.. I am used to C and didn't notice it.. two hours lost because of that! so embarrasing.. Ouch!
    – jap jap
    Nov 19 at 19:16






  • 1




    If you use an editor which has some sort of makefile editing mode it will show comments in a different color. It will also do better at handling TAB vs. spaces, typically. For debugging it can be helpful to add $(info ...) functions into your makefiles to show variable values at various times etc. Adding these into common.mk would have probably let you know that it wasn't being parsed at all, pretty quickly.
    – MadScientist
    Nov 19 at 19:21













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to use a common.mk file for common variable definitions and include this file in other Makefiles but in the first example I've done it is not working so I think that I have misunderstood something.



This is the Makefile:



CC=gcc
CFLAGS=-g -Wall
LIB_FLAGS=-L/usr/local/lib/ -lcgreen

BUILDDIR=$(CURDIR)/build
SRC=$(wildcard *.c)

export BUILDDIR
export CFLAGS
export LIB_FLAGS

#include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined

unittests: dir externals $(OBJ)
@echo "SRC: $(SRC)"
@echo "OBJ: $(OBJ)" # THIS PRINTS OBJ AS EMPTY <------------------------------
$(CC) $(BUILDDIR)/*.o $(LIB_FLAGS) -o $(BUILDDIR)/unittests
$(BUILDDIR)/unittests

externals:
@$(MAKE) -C lib1 -f lib1.mk

dir:
mkdir -p $(BUILDDIR)


This is the common.mk file:



 OBJ=$(patsubst %.c, $(BUILDDIR)/%.o, $(notdir $(SRC)))

$(BUILDDIR)/%.o: %.c
@echo "File: "$<
$(CC) -c $(CFLAGS) $< -o $@

clean:
rm -f $(OBJ)


So I was expecting that the OBJ variable in the main Makefile had a object files list but it is empty and I don't understand why. Isn't it including the common.mk file the same as copying it's content into the Makefile?



Thanks










share|improve this question













I want to use a common.mk file for common variable definitions and include this file in other Makefiles but in the first example I've done it is not working so I think that I have misunderstood something.



This is the Makefile:



CC=gcc
CFLAGS=-g -Wall
LIB_FLAGS=-L/usr/local/lib/ -lcgreen

BUILDDIR=$(CURDIR)/build
SRC=$(wildcard *.c)

export BUILDDIR
export CFLAGS
export LIB_FLAGS

#include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined

unittests: dir externals $(OBJ)
@echo "SRC: $(SRC)"
@echo "OBJ: $(OBJ)" # THIS PRINTS OBJ AS EMPTY <------------------------------
$(CC) $(BUILDDIR)/*.o $(LIB_FLAGS) -o $(BUILDDIR)/unittests
$(BUILDDIR)/unittests

externals:
@$(MAKE) -C lib1 -f lib1.mk

dir:
mkdir -p $(BUILDDIR)


This is the common.mk file:



 OBJ=$(patsubst %.c, $(BUILDDIR)/%.o, $(notdir $(SRC)))

$(BUILDDIR)/%.o: %.c
@echo "File: "$<
$(CC) -c $(CFLAGS) $< -o $@

clean:
rm -f $(OBJ)


So I was expecting that the OBJ variable in the main Makefile had a object files list but it is empty and I don't understand why. Isn't it including the common.mk file the same as copying it's content into the Makefile?



Thanks







makefile gnu-make






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 19:10









jap jap

9911




9911








  • 1




    #include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined doesn't include anything. It starts with #, so it's just a comment.
    – melpomene
    Nov 19 at 19:13










  • @melpomene Thanks.. I am used to C and didn't notice it.. two hours lost because of that! so embarrasing.. Ouch!
    – jap jap
    Nov 19 at 19:16






  • 1




    If you use an editor which has some sort of makefile editing mode it will show comments in a different color. It will also do better at handling TAB vs. spaces, typically. For debugging it can be helpful to add $(info ...) functions into your makefiles to show variable values at various times etc. Adding these into common.mk would have probably let you know that it wasn't being parsed at all, pretty quickly.
    – MadScientist
    Nov 19 at 19:21














  • 1




    #include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined doesn't include anything. It starts with #, so it's just a comment.
    – melpomene
    Nov 19 at 19:13










  • @melpomene Thanks.. I am used to C and didn't notice it.. two hours lost because of that! so embarrasing.. Ouch!
    – jap jap
    Nov 19 at 19:16






  • 1




    If you use an editor which has some sort of makefile editing mode it will show comments in a different color. It will also do better at handling TAB vs. spaces, typically. For debugging it can be helpful to add $(info ...) functions into your makefiles to show variable values at various times etc. Adding these into common.mk would have probably let you know that it wasn't being parsed at all, pretty quickly.
    – MadScientist
    Nov 19 at 19:21








1




1




#include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined doesn't include anything. It starts with #, so it's just a comment.
– melpomene
Nov 19 at 19:13




#include common.mk # HERE I INCLUDE THE common.mk where OBJ is defined doesn't include anything. It starts with #, so it's just a comment.
– melpomene
Nov 19 at 19:13












@melpomene Thanks.. I am used to C and didn't notice it.. two hours lost because of that! so embarrasing.. Ouch!
– jap jap
Nov 19 at 19:16




@melpomene Thanks.. I am used to C and didn't notice it.. two hours lost because of that! so embarrasing.. Ouch!
– jap jap
Nov 19 at 19:16




1




1




If you use an editor which has some sort of makefile editing mode it will show comments in a different color. It will also do better at handling TAB vs. spaces, typically. For debugging it can be helpful to add $(info ...) functions into your makefiles to show variable values at various times etc. Adding these into common.mk would have probably let you know that it wasn't being parsed at all, pretty quickly.
– MadScientist
Nov 19 at 19:21




If you use an editor which has some sort of makefile editing mode it will show comments in a different color. It will also do better at handling TAB vs. spaces, typically. For debugging it can be helpful to add $(info ...) functions into your makefiles to show variable values at various times etc. Adding these into common.mk would have probably let you know that it wasn't being parsed at all, pretty quickly.
– MadScientist
Nov 19 at 19:21












1 Answer
1






active

oldest

votes

















up vote
0
down vote













As @melpomene pointed out I was including the file like it is done in C and therefore commenting it out!



Sorry for wasting your time..






share|improve this answer





















    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',
    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%2f53381132%2fmakefile-variables-are-not-included-from-other-makefile%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








    up vote
    0
    down vote













    As @melpomene pointed out I was including the file like it is done in C and therefore commenting it out!



    Sorry for wasting your time..






    share|improve this answer

























      up vote
      0
      down vote













      As @melpomene pointed out I was including the file like it is done in C and therefore commenting it out!



      Sorry for wasting your time..






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        As @melpomene pointed out I was including the file like it is done in C and therefore commenting it out!



        Sorry for wasting your time..






        share|improve this answer












        As @melpomene pointed out I was including the file like it is done in C and therefore commenting it out!



        Sorry for wasting your time..







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 19:19









        jap jap

        9911




        9911






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53381132%2fmakefile-variables-are-not-included-from-other-makefile%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







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]