Define variables used in Makefile from command line

I encountered this problem when I use a lot of Macros and it is annoying I used to define them in code or Makefile with -DVar (Var is the name of the variable flag).

This trick lets you define the variable as an environment variable in command line, and add it in through the command line without modification of the code.

First, each compile command in Makefile should have a CFLAGS, like the following

CFLAGS = -g -O3 -ipo -std=gnu++11

all:

${CXX} -I. pagerankCsrPullCilk.cpp ${CFLAGS} -o pagerankCsrPullCilk.o

Then add this before the commands

ifeq (${DEBUG},1)

CFLAGS+=-DDEBUG

endif

And now, you can define the DEBUG flag with the following command

DEBUG=1 make

Advertisements
This entry was posted in Tools and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s