Rule to set a variable in Makefile not working as expected

aarelovich

I'm writing a makefile that can compile different projects depending on the rule used. For this I need to set certain variables to set paths and generate the right output files.

This is the section that is currently not working:

bubblesort: OUTPROG = bubblesort
bubblesort: APP_PATH    = $(SRCS)/bubblesort
bubblesort: OBJS    = ../programs/bubblesort/bubblesort.o
bubblesort: $(COMMON_OBJS) $(OBJS)
    @ echo "<-------------------- Making Bubblesort -------------------->"
    @ echo "<-------------------- Linking files -------------------->"
    $(LD) $(COMMON_OBJS) $(OBJS) $(LDFLAGS)
    @ echo "<-------------------- ELF to Binary File -------------------->"
    $(OC) $(OCFLAGS) $(TARGET) $(BINOUT)
    @ echo "<-------------------- Binary to Verilog Conversion -------------------->"
    $(R2V) $(R2VPARAMS) 


# This is the rule to transform any c code to object file via compilation.
%.o : %.c
    @ echo "<-------------------- Compiling C Source Files -------------------->"
    $(CC) $(CFLAGS_APP) $< -o $@

What happens is that the compilation of the c sources to generate the .o is never invoked (the %.o: %.c rule). However this works:

OBJS        = ../programs/bubblesort/bubblesort.o
bubblesort: OUTPROG = bubblesort
bubblesort: APP_PATH    = $(SRCS)/bubblesort
bubblesort: $(COMMON_OBJS) $(OBJS)
    @ echo "<-------------------- Making Bubblesort -------------------->"
    @ echo "<-------------------- Linking files -------------------->"
    $(LD) $(COMMON_OBJS) $(OBJS) $(LDFLAGS)
    @ echo "<-------------------- ELF to Binary File -------------------->"
    $(OC) $(OCFLAGS) $(TARGET) $(BINOUT)
    @ echo "<-------------------- Binary to Verilog Conversion -------------------->"
    $(R2V) $(R2VPARAMS) 

My question is simply why? I would appreciate any input.

EDIT:

Also how can I make the prerequisite depend on the rule invoked?

EDIT 2:

Following the advice from Etan Raiser I modified my code to look like this:

bubblesort: OUTPROG = bubblesort
bubblesort: APP_PATH    = $(SRCS)/bubblesort
bubblesort: $(COMMON_OBJS) ../programs/bubblesort/bubblesort.o creation

creation: $^
    @ echo "<-------------------- Making in general -------------------->"
    @ echo "<-------------------- Linking files -------------------->"
    $(LD) $^ $(LDFLAGS)
    @ echo "<-------------------- ELF to Binary File -------------------->"
    $(OC) $(OCFLAGS) $(TARGET) $(BINOUT)
    @ echo "<-------------------- Binary to Verilog Conversion -------------------->"
    $(R2V) $(R2VPARAMS) 
    @ echo "<-------------------- Creating Obj Dump -------------------->"
    $(OD) $(ODFLAGS) $(TARGET) > $(APP_PATH)/$(OUTPROG)_DUMP.txt
Etan Reisner

It is a question of variable expansion time. (See How make Reads a Makefile.)

The target line bubblesort: $(COMMON_OBJS) $(OBJS) is expanded immediately.

The target-specific variable line bubblesort: OBJS = ../programs/bubblesort/bubblesort.o isn't "executed" (actually doing the assignment) until the bubblesort target is being run.

Instead of this (which doesn't work):

bubblesort: OUTPROG = bubblesort
bubblesort: APP_PATH    = $(SRCS)/bubblesort
bubblesort: OBJS    = ../programs/bubblesort/bubblesort.o
bubblesort: $(COMMON_OBJS) $(OBJS)
    @ echo "<-------------------- Making Bubblesort -------------------->"
    @ echo "<-------------------- Linking files -------------------->"
    $(LD) $(COMMON_OBJS) $(OBJS) $(LDFLAGS)
    @ echo "<-------------------- ELF to Binary File -------------------->"
    $(OC) $(OCFLAGS) $(TARGET) $(BINOUT)
    @ echo "<-------------------- Binary to Verilog Conversion -------------------->"
    $(R2V) $(R2VPARAMS) 

and which needs the $(OBJ) variable for the $(LD) call.

You can use:

bubblesort: OUTPROG = bubblesort
bubblesort: APP_PATH    = $(SRCS)/bubblesort
bubblesort: $(COMMON_OBJS) ../programs/bubblesort/bubblesort.o
    @ echo "<-------------------- Making Bubblesort -------------------->"
    @ echo "<-------------------- Linking files -------------------->"
    $(LD) $^ $(LDFLAGS)
    @ echo "<-------------------- ELF to Binary File -------------------->"
    $(OC) $(OCFLAGS) $(TARGET) $(BINOUT)
    @ echo "<-------------------- Binary to Verilog Conversion -------------------->"
    $(R2V) $(R2VPARAMS)

Which works and uses the built-in $^ automatic variable for its intended purpose.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Makefile generic rule not working

From Dev

Firestore rule not working as expected

From Dev

Why is my makefile not working as expected?

From Dev

GNU Make - Set MAKEFILE variable from shell command output within a rule/target

From Dev

Makefile set variable in target

From Dev

Setting Makefile variable to result of command in rule

From Dev

Use "at variable" ($@) on the right hand side of a rule in makefile

From Dev

Replica Set not working as expected

From Dev

Replica Set not working as expected

From Dev

PHP is variable not working as expected

From Dev

PHP is variable not working as expected

From Dev

Python variable not working as expected

From Dev

Set variable in a rule and have it available in another rule

From Dev

Set a variable from within a rule

From Dev

multiline variable definitions not working in Makefile

From Dev

Makefile -- error out if variable not set

From Dev

Check if environmental variable is set in makefile

From Dev

set -u usage not working as expected

From Dev

"DO UPDATE SET" not working as expected

From Dev

variable in bash script not working as expected

From Dev

using a variable mysql not working as expected?

From Dev

Change a make variable, and call another rule, from a recipe in same Makefile?

From Dev

Forcing certain environment variable to be defined when a specific Makefile rule is invoked

From Dev

CMake SET command not working as expected (CMake not setting value of predefined cached variable)

From Dev

CMake SET command not working as expected (CMake not setting value of predefined cached variable)

From Dev

AIX make/makefile : variable assignment is not working

From Java

How to set child process' environment variable in Makefile

From Dev

Makefile: read input variable and set several variables

From Dev

Is it possible to set environment variable through a makefile?

Related Related

  1. 1

    Makefile generic rule not working

  2. 2

    Firestore rule not working as expected

  3. 3

    Why is my makefile not working as expected?

  4. 4

    GNU Make - Set MAKEFILE variable from shell command output within a rule/target

  5. 5

    Makefile set variable in target

  6. 6

    Setting Makefile variable to result of command in rule

  7. 7

    Use "at variable" ($@) on the right hand side of a rule in makefile

  8. 8

    Replica Set not working as expected

  9. 9

    Replica Set not working as expected

  10. 10

    PHP is variable not working as expected

  11. 11

    PHP is variable not working as expected

  12. 12

    Python variable not working as expected

  13. 13

    Set variable in a rule and have it available in another rule

  14. 14

    Set a variable from within a rule

  15. 15

    multiline variable definitions not working in Makefile

  16. 16

    Makefile -- error out if variable not set

  17. 17

    Check if environmental variable is set in makefile

  18. 18

    set -u usage not working as expected

  19. 19

    "DO UPDATE SET" not working as expected

  20. 20

    variable in bash script not working as expected

  21. 21

    using a variable mysql not working as expected?

  22. 22

    Change a make variable, and call another rule, from a recipe in same Makefile?

  23. 23

    Forcing certain environment variable to be defined when a specific Makefile rule is invoked

  24. 24

    CMake SET command not working as expected (CMake not setting value of predefined cached variable)

  25. 25

    CMake SET command not working as expected (CMake not setting value of predefined cached variable)

  26. 26

    AIX make/makefile : variable assignment is not working

  27. 27

    How to set child process' environment variable in Makefile

  28. 28

    Makefile: read input variable and set several variables

  29. 29

    Is it possible to set environment variable through a makefile?

HotTag

Archive