C like #define in a source file

⚓ Rust    📅 2025-09-25    👤 surdeus    👁️ 8      

surdeus

Warning

This post was published 36 days ago. The information described in this article may have changed.

so in C we often have an include file that has our debug output routines.

in c we do this

      #define ENABLE_DEBUG. 0 //0. disables, 1 enables
      #include “debug_macros.h”

that debug macros file based on the define either a) has a bunch of debug functions or b) the #define macros turn the debug calls into null statements, please do not suggest dbg!() that has other issues on our embedded target

using #cfg() everywhere is a problem/messy because there would be hundreds of these in the module

we specifically enable/disable debug this way in the source file on purpose because a command line define is way to heavy handed it turns on far to many debug things in far to many files

and often you only want it enabled for a very few files anyway (the few you are working on at the moment)

i have seen the rust macro_rules! and think that is part of the solution but i still need to control the expansion some how on a per file basis

question #1 can some one point me to some examples?

question #2 how do you set a config option in a source file and specifically not the command line or cargo file

====
next is debug vrs release…. we make extensive use of the c language pargma to enable or disable optimization for a single file or two.

ie in an embedded environment we have very limited code/ram space - it fits if the code is optimized it does not if it is optimized. this also makes it impossibly hard to debug

so we have a macro OPTIMIZE_THIS_FILE() it basically turns on optimization for the file using a pragma

we place it at the top of every file and the code fits in the target :-> yea!

and for that one or two files you are working on (debugging) you comment out the macro and rebuild…. because everything else is optimized it fits! yea! and the one or two problem files while bigger… it fits! the rest are still optimized and the debugger has a reasonable experience.

how can i accomplish this with rust?

1 post - 1 participant

Read full topic

🏷️ Rust_feed