Macros in main program- how to call them outside the program

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

hi all..

I found a macro in main loop which never been defined before the main program..
ex:
in the file main.c

int main(void)
{
#ifdef ABC
ABC_Init();
#endif
}

in the header file also this sort of a #ifdef statements are there with the same macro but this macro has never been defined in the program itself.

For example in order to get ABC_init() done how can I compile or run the program.
Is there any way to define this macro before the compilation/running outside from the main program.. ?

If there is any reference in the web please be kind enough to let me know too.. since I couldn't find any tutorial on this regard.

Thanks a lot.
BR
Kanishka

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

It is not macro but conditional compilation.
If you write (out of main)

#define ABC

then ABC_Init() will be compiled.

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

Maybe try:

http://www.google.co.uk/#sclient...

It seems to me that these days people are being taught C or C++ without ever being taught anything about the C preprocessor. You have to realise that it's a completely separate (mainly string substitution) step performed on the code before the C compiler ever sees it. As such the C compiler itself never "sees" lines starting with '#' and there's certainly no way you ever "call" a macro. The text of it will be expanded inline wherever it occurs.

Cliff

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

Hi thanks for the reply...

I found the answer with Visiovian's reply..

Thanks a lot for that..
I understood that if the macro is predefined, the code inside the conditional block can be executed and the macro will be substituted by the macro definition. but the problem I faced was its never been defined in the program.

Found that it can be defined/undifined with options -D and -U respectively and found the trick in the makefile.

http://en.wikipedia.org/wiki/C_p...
http://gcc.gnu.org/onlinedocs/cp...

makefile:

# If ABC-Support is enabled
ifeq ($(EXT), ABC)
CPPDEFS += -DABC
endif

Thanks for directing me to the right direction with the right word..
BR
Kanishka

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

Quote:
I understood that if the macro is predefined, the code inside the conditional block can be executed and the macro will be substituted by the macro definition. but the problem I faced was its never been defined in the program.
I am not quite sure if you understood.
Seems you tend to call "macro" every thing that starts with "#define" ?

  #ifdef ABC
  ABC_Init();
  #endif

This conditional block tells the preprocessor:
If the expression (constant) ABC is somewhere defined, then put line "ABC_Init();" to the code.
No macro here.

An example of conditional compilation:
When debugging the code I watch values of variables in a PC terminal.
After debugging is done, I simply comment the line
#define DEBUG
to clear the debug code away.

//pseudocode

#define DEBUG

uint16_t  vara,
          varb,
          varc;

main()
{
   ...
   ...
   ...
   #ifdef DEBUG
   print (vara);
   #endif
   ...
   ...
   #ifdef DEBUG
   print (varb);
   #endif
   ...
   ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sir,
where is the following code used? :
#if 0
#endif
As I understand from your discussion that it is an compile time code and not run time code, what is the need of such a code once your debugging phase is over, as it will never be executed in the runtime? Should we remove it at the time of final compilation?

Why memorize anything which you can easily pen down on a piece of paper or get from a book in less than two minutes? - Albert Einstein on being asked why cant he remember his phone number!!!

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

Lets say you have conditional compile directives for debug and production code. Why remove the extra work you've done? Frequently one is required to debug code after it has been released. The magic of conditional compile is that only what is compiled ends up in the binary object.

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

superconductor wrote:
Should we remove it at the time of final compilation?
Yes, but not with deleting all #ifdef's in the code.
It is enough to delete the line
#define DEBUG
or comment it
//#define DEBUG

In future you may need to debug the code again,
as Kartman wrote.
Then you only uncomment the line.

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

If you use a decent editor it will grey any non-active sections under #ifdef or "#if 0" so it's easy to see which bits of the code are active and which are not.

BTW the great use of "#if 0" in particular is when you want to disable a large section of the code. It's far better than /* .. */ or // on each line as it will not get confused by any other commenting characters in the code. The entire section is removed before the compiler itself ever gets to see the source.

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

how are the definitions converted after compilation? I ask because does the length of the identifier matter? Is it converted to a symbol?
Will the following lengths make any difference :
# define YOU_ARE_A_GENTLEMAN
or a shorter :
# define YOU_ARE_A_MAN

Thanks!

Why memorize anything which you can easily pen down on a piece of paper or get from a book in less than two minutes? - Albert Einstein on being asked why cant he remember his phone number!!!

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

It makes no difference. Also your question:

Quote:

how are the definitions converted after compilation?

makes no sense whatsoever. The compilation phase never knows ANYTHING about pre-processor macros. They are already replaced at the preprocessor stage.

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

In that case I re-phrase,
How are definitions processed at the preprocessor stage?
What are they replaced with? Symbols?

Thanks!

Why memorize anything which you can easily pen down on a piece of paper or get from a book in less than two minutes? - Albert Einstein on being asked why cant he remember his phone number!!!

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

Why don't you get a beginners guide to C programming (or Google). The operation of the C Pre-Processor is well documented in many places and I don't plan to explain the whole thing in a post here.

A quick Google leads to:

http://gcc.gnu.org/onlinedocs/cpp/

While that is the manual specific to CPP in GCC most of what it says should apply to the CPP in any C compiler (as they are all supposed to work the same).

Oh and most C compilers support -E (instead of -c) which means output the preprocessed files - don't compile them.

One GCC option I particularly like is --save-temps. If you use this then for every file.c you will get a file.i and a file.s - the file.i is the preprocessed C and the file.s is the generated assembler source.

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

Here's an example:

#include 

#define COUNT 10
#define CODE_PATH1

int main(void){
	uint8_t i;
	for (i = 0; i < COUNT; i++) {
#ifdef CODE_PATH0
		PORTB = i;
#elif defined(CODE_PATH1)
		PORTC = i;
#else
		PORTD = i;
#endif	
	}
}

After pre-processing, but before compilation this becomes:




int main(void){
 uint8_t i;
 for (i = 0; i < 10; i++) {



  (*(volatile uint8_t *)((0x08) + 0x20)) = i;



 }
}

(this is built for mega48 where PORTC = 0x08)