Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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();


}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading