Debugging using makefile flags in C -


i need set way debug program make file. specifically, when type make -b flag=-dndebug need program run normally. when flag not present need few assert() commands throughout code run.

to clarify need know how check if flag not present within c code, i'd assume has #ifndef, don't know go there.

forgive ignorance, response appreciated!

assuming talking assert macro standard library (#defined in <assert.h>) don't have anything. library takes care of ndebug flag.

if want make own code things if macro / not #defined, use #ifdef suspect in question.

for example, might have condition complex put single assert expression want variable it. if assert expands nothing, don't want value computed. might use this.

int questionable(const int * numbers, size_t length) { #ifndef ndebug   /* assert numbers not same. */   int min = int_max;   int max = int_min;   size_t i;   (i = 0; < length; ++i)     {       if (numbers[i] < min)         min = numbers[i];       if (numbers[i] > max)         max = numbers[i];     }   assert(length >= 2);   assert(max > min); #endif   /* you're supposed numbers... */   return 0; } 

be warned coding style makes hard read code , asking heisenbugs extremely difficult debug. better way express use functions.

/* 1st helper function */ static int minimum(const int * numbers, size_t length) {   int min = int_max;   size_t i;   (i = 0; < length; ++i)     {       if (numbers[i] < min)         min = numbers[i];     }   return min; }  /* 2nd helper function */ static int maximum(const int * numbers, size_t length) {   int max = int_min;   size_t i;   (i = 0; < length; ++i)     {       if (numbers[i] > max)         max = numbers[i];     }   return max; }  /* actual function */ int better(const int * numbers, int length) {   /* no nasty `#ifdef`s */   assert(length >= 2);   assert(minimum(numbers, length) < maximum(numbers, length));   /* you're supposed numbers... */   return 0; } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -