#define from make's command line

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good day!

I've something like that in my code:

Quote:

#ifdef DEBUG
//some debug code
#endif

I know that it is possible to specify define using -D option for gcc, but my goal is to be able to run from command-line

Quote:

make DEBUG=TRUE

And make will add -D option for gcc.

Is it possible?

Thanks in advance!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can affect make variables from the command line.
make CFLAGS=banananana ...
might do what you want.
Be careful when filling in banananana.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try:

make CFLAGS=-DDEBUG

EDIT: it appears to work:

#include 
#include 
#include 

const char string[] PROGMEM = "Hello World";

#ifdef DEBUG
#error "it works"
#endif

int main(void) { 
	while(1) {
D:\test>make CFLAGS=-DDEBUG

-------- begin --------
avr-gcc (WinAVR 20100110) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Size before:
AVR Memory Usage
----------------
Device: atmega48

Program:     686 bytes (16.7% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)



Compiling C: test.c
avr-gcc -c -mmcu=atmega48 -I. -DDEBUG -MMD -MP -MF .dep/test.o.d test.c -o test.
o
test.c:8:2: error: #error "it works"
make: *** [test.o] Error 1

(without that CFLAGS= the code does not hit the #error)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It works, but it also removes whatever else was in CFLAGS.
One has, for example, the default optimization level.
Does += work on the command line?

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> Does += work on the command line?

No, I don't think it does. What *does* work though is:

Makefile:

DEBUG=0

...
CFLAGS += -DDEBUG=${DEBUG}

Call it as:

make DEBUG=1

and it will pass the 1 (aka. "true") instead of 0 for the DEBUG macro.
In your code, use:

#if DEBUG
#  warning "Debugging is enabled now, expect funny results"
#endif

(rather than just #ifdef)

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, the way described in the last message is what I was looking for!

Thanks a lot!