Chandra
Chandra

Reputation: 131

How to define a macro in a header file from the makefile

I have written my own header file for checking assertions myassert.h, similar to the assert.h header and I include this in another file, say project_file.h. I also have the NDEBUG macro which when defined before the inclusion of myassert.h, disables the checking of the assertions. However, I dont want to turn off the assertion checking by defining NDEBUG at the beginning of project_file.h. Instead I want to have a flag in the makefile so that during compilation, the user can set the flag that will automatically disable/enable the assertions checking feature. The file project_file.h is just one file in a huge project. Can anyone please tell me how to do this? I have checked the other answers but I did not quite understand how to make it work.

Thank you very much.

Upvotes: 0

Views: 1337

Answers (1)

blockchaindev
blockchaindev

Reputation: 3196

What you're looking for is called a preprocessor definition. You should pass -DNDEBUG to the gcc options.

For more information about the -D option: http://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html

There are two ways to use -D: -D name, -D name=definition.

Then in your code you would use:

#ifndef NDEBUG
    // ...
#endif

or

#if NDEBUG!=0
    // ...
#endif

Upvotes: 3

Related Questions