0

I have the following C code:

file.c:

int i=VAR;

which is compiled using a makefile:

CFLAGS =+ -DVAR=10

but instead of compiling the file using just "make" and relying on the variable in the makefile, I would like to change the variable each time I compile the program using the command line.
For example, I expect the following command line to work:

make CFLAGS=-DVAR=10
Bojangles
  • 1
  • 4
  • 1
    Possible duplicate of [Passing additional variables from command line to make](https://stackoverflow.com/questions/2826029/passing-additional-variables-from-command-line-to-make) – bolov Jun 18 '18 at 11:26
  • 1
    You say you expect it to work, which implies it didn't do what you wanted, but you don't say that it didn't work, you don't show what happened when you tried it, and you don't explain what went wrong. – MadScientist Jun 18 '18 at 12:24

1 Answers1

2

First, this syntax is wrong:

CFLAGS =+ -DVAR=10

I assume you meant += here not =+. Second, the commands you ran should work, for some definition of work, so without information on what happened and why it wasn't what you wanted we can't help.

However, if it were me I'd do it differently; I'd do something like this:

VAR := 10

CFLAGS += -DVAR=$(VAR)

then when I wanted to change it I would run:

make VAR=20
MadScientist
  • 92,819
  • 9
  • 109
  • 136