1

I saw previous SO posts 1, 2 etc. I want to pass a preprocessor directive at compile time. With scons, I could do:

num_times = ARGUMENTS.get('c', 1)
env.Append(CCFLAGS = '-DNUM_TIMES=%d' % int(num_times))

I hope, it should also be possible using make. I want to issue

make c=4

or something like that. Can someone suggest a method. I am compiling a folder, which has subfolders with their own makefiles. Thanks in advance.

Community
  • 1
  • 1
user984260
  • 3,401
  • 5
  • 25
  • 38

1 Answers1

1

One simple approach is to do:

CFLAGS += -DNUM_TIMES=$(c)
export CFLAGS

all:
    $(MAKE) -C your_subfolder

call with make c=2, and not to touch CFLAGS in the sub-folder makefiles.

Another is to have a Makefile part in your root folder with all the common settings that you include in your subdirectory makefiles (with include). ($(MAKE) ensures that the command line arguments you gave to make will also be passed to sub-makes.)

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Thanks. Is it OK, if the sub-makefiles do: CFLAGS += $(CXXFLAGS) – user984260 Mar 27 '12 at 19:57
  • Just make sure you don't do the same thing in the main makefile and in the subfile and at the same time export in the main - you'll get things doubled up in your variables, which could be problematic. The best is (probably) not to `export` but have a common make snippet that you include everywhere with the settings you want. – Mat Mar 27 '12 at 20:01