c++cmdline-args

Is it possible to disable code paths with command line arguments in C++?


I have a debug drawing flag in my game project. This flag determines whether Box2d's DebugDraw is drawing before the end of each frame. Is it possible to use something like:

#ifdef 'debug drawing flag'                       
//do debug drawing
#else
//skip

Or would it make more sense to have an if statement to check if that value was assigned?

//With a single command line arg
int main(int argc, char* argv []){
    //game initialization
    .
    .
    .

    //Check if its there and set flag
    uint8_t debugDrawFlag = (argc > 0 && strcmp("debugDraw", argv[0]) == 0) ? 1 : 0
           

    .
    .
    .
    if(debugDrawFlag)
        physicsWorld->DebugDraw();


}


Solution

  • #ifdef only works at compile-time, not at runtime.

    Most compilers allow you to define custom symbols on the command-line while compiling, for instance with a -D switch or equivalent.

    So, the question comes down to - do you want to specify your drawing flag at compile-time when your app is being built, or do you want to control your drawing mode dynamically when your app is being run? If the former, use #ifdef. If the latter, use argv.