Can I get the C++ preprocessor to send output during compilation? -


i have been debugging particularly insidious bug believe caused unexpected changes stem different behavior when different headers included (or not).

this not structure of code let's take @ scenario:

#include "newly_created_header_which_accidentally_undefines_some_define.h"  // ...  #ifdef some_define     code_which_i_believe_i_am_always_running(); #else      code_which_fails_which_i_have_forgotten_about(); // runtime error stack traces here, don't know this... or maybe it's strange linker error #endif 

i search through git commits , narrow down cause of bug, compiling , running code countless times, find after several hours difference required causing bug inclusion of appears benign , unrelated header.

perhaps great argument why preprocessor sucks.

but it. preprocessor cool because lets make shortcuts. it's of these shortcuts, when not used carefully, bite in butt pretty hard.

so @ juncture have helped if use directive #echo "running old crashy code" i'll able see during compilation tipped off start investigating why some_define not defined.

as far know straightforward way of determining if some_define defined

#ifndef some_define     printf("some_define not defined!!\n"); 

this surely job done there no reason task performed @ runtime because entirely determined @ compile-time. i'd see @ compile-time.

that being said, in situation, using print (or log or throwing exception) may acceptable thing because won't care slowing down or cluttering questionable code. doesn't apply if have instance 2 code paths both of important, , want know @ compile-time 1 being activated. i'd have worry running code preprocessor-conditioned print @ beginning of program.

this long-winded way of asking question, "can echo string output during compilation using preprocessor directive?"

if use #error directive, output printed directly , compilation stop:

$ make days_in_month cc     days_in_month.c   -o days_in_month days_in_month.c:2:2: error: #error "ugly!" make: *** [days_in_month] error 1 $  

this might not quite wanted, gets job done quickly.

$ cat days_in_month.c #include <stdio.h> #error "ugly!" ... 

if wish processing continue, can use #warning:

$ make days_in_month cc     days_in_month.c   -o days_in_month days_in_month.c:2:2: warning: #warning "ugly!" [-wcpp] 
$ head days_in_month.c  #include <stdio.h> #warning "ugly!" 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -