pre defined variables

Hi

First I want to explain the scenerio :
In my project I want to control the printing of some messages. Thats why I use
#ifdef MESSAGE
------------print message--------------
#endif

And when I compile the program with -DMESSAGE option, it shows all message and without -DMESSAGE option it shows nothing. thats fine.

But this thing I want to control at run-time, mean if I execute the program like

./demo MESSAGE (it should show all the messages)
./demo (it should not show any message)

thanks

Just a thought.

Why dont you define MESSAGE in the env and make a check for it in your application using the getenv("MESSAGE") call. That should simplify things.

Just to confirm that there is absolutely no way to change the preprocessor at runtime. The preprocessor only runs once, during compile time; to change what the preprocessor sees, you must recompile.

thanks for your valuable explaination.

Now I am controlling the printing the message with normal if-else.

And one more thing : Should logging controlled with nornal if-else or preprocessor controled if-else ?

This is what we do in one of our application.

We have a config file which keeps all the user's preferences.

If there is an entry like

DEBUG=1

then in-side our code all those lines in

#ifdef DEBUG
print some debug statements 
#endif

gets printed.

vino

thanks vino.

But what I want to ask is what is the STANDARD way to do this thing?

your way or by normal if-else.

I doubt there is a standard way. You can do it the way you like.

I've seen constructs like these in a lot of programs:

/**
 * mydebug.h -- freeware
 */

/* Prevent mydebug.h from being included more than once */
#ifndef __MYDEBUG_H__
#define __MYDEBUG_H__

/* Makes these functions work even if you include and use this in a .cpp file */
#ifdef __cplusplus
extern "C" {
#endif

/* Don't call directly, use DEBUG_PRINT(("something")) macros */
void _debug_print(const char *cmdstr, ...);
void _info_print(const char *cmdstr, ...);

#ifdef __cplusplus
}
#endif

/**
 * INFO_PRINT always calls _info_print, even if it might NOT actually print anything,
 * so use it sparingly, it'll bloat the final program.
 */
#define INFO_PRINT(X) _info_print X

/**
 * Will ONLY be called when DEBUG is defined, should magically disappear
 * otherwise.  Use all you want, will not bloat non-DEBUG builds.
 */
#ifdef DEBUG
#define DEBUG_PRINT(X) _debug_print X
#else
#define DEBUG_PRINT(X)
#endif

#endif/*__MYDEBUG_H__*/
/**
 * mydebug.c -- freeware
 */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "mydebug.h"

void _debug_print(const char *cmdstr, ...)
{
  va_list ap;

  va_start(ap,cmdstr);
    vfprintf(stderr,cmdstr,ap);
  va_end(ap);
}

void _info_print(const char *cmdstr, ...)
{
  va_list ap;

  /**
   * When DEBUG is undefined, it checks for the DEBUG environment variable.
   * Otherwise, it always prints.
   */

#ifndef DEBUG
  if(getenv("DEBUG") == NULL) return;
#endif

  va_start(ap,cmdstr);
    vfprintf(stderr,cmdstr,ap);
  va_end(ap);
}

This moves all the crazy preprocessor things into a header file and source file so you don't have to worry about whether they should be printing or not.

These functions also behave exactly like printf, because they just pass all their arguments to printf.

/**
 * debugtest.c -- freeware
 */
#include "mydebug.h"

int main()
{
  DEBUG_PRINT(("This should only print when DEBUG's defined in preprocessor %d\n",5));
  INFO_PRINT(("If DEBUG is defined, this always prints\n"));
  INFO_PRINT(("If DEBUG isn't defined, it only prints if DEBUG is in the environment\n"));
  return(0);
}

Note the crazy double brackets. These are there because most compilers cannot handle variable numbers of arguments inside preprocessor macros; the whole bracketed mess becomes one paramater, so DEBUG_PRINT(("something")) becomes _debug_print ("something") .

That I've seen this reimplimented time and time again in so many programs suggests to me there's no "standard" way.