We use #warning to show compile message, like this:
#warning Are you sure it is correct?
Is is possible redefine the #warning and can choice if enable or disable it, like this:
#ifdef ACTIVE_MY_WARNING
#define #my_warning #warning
#else
#define #my_warning
#endif
We can do it in C or not?
No, it's not possible to redefine preprocessor keywords (#if, #define, #ifdef, etc).
Your best shot is to have a parser that can replace your code with:
#ifdef ACTIVE_MY_WARNING
#warning Are you sure it is correct?
#endif
You tag it in C
and as per my knowledge there is no
preprocessor live #warning in c.
My question for you ans " We can do it in C or not? "
is No.
Related
How can I define a directive as an other directive in C ?
ex : I want to define
#define #warning #warn
I get an error
error #41: expected an identifier
for compilers and targets for example, some compilers recognize #warning and other recognize #warn.
I already have a way to do this but I want to make it generic
#if PLATFORMS_TOOLCHAIN == PLATFORMS_ticgt
#warn "RAM is not initialized at startup, test will possibly fail"
#else
#warning "RAM is not initialized at startup, test will possibly fail"
#endif
There's no way to create your own preprocessor directives. The #define directive allows you to define new source symbols only.
What you're currently doing is the proper way to handle differing directives on different compilers.
In general it's not possible, however you can effective generate #pragma directives with the _Pragma keyword.
#define PRAGMA(...) _Pragma(#__VA_ARGS__)
PRAGMA(message "my message")
//as if you wrote: #pragma message "my message"
With this in mind, your compiler might have pragmas that allow you to emulate #warn or #error
(#pragma GCC warning "message" or #pragma GCC error "message").
If your compiler doesn't have such pragmas, you can always fail a compilation by expanding to something relatively context-free that's not compilable, e.g., char ERROR[-1]; (negative array sizes are illegal).
I have some code which I am developing to compile on both MS Visual C 2010 compiler and an Embedded ARM compiler. Also I want to print some warnings at compile time to remind me that I have some testing code in place. Unfortunately MS do messages with #pragma instead of #warning so I can't just use the same pre-processor directive in both compilers.
I could do this every where:
#ifdef _MSC_VER
#pragma message("Something to say....")
#else
#warning "Something to say...."
#endif
But it would be nicer to have a macro encapsulate this, eg:
#define ccWarnMessage(m) \
#ifdef _MSC_VER \
#pragma message(m) \
#else \
#warning m \
#endif
This does not work but is there a way to do it in a neat way ?
I would not use warnings for it. As I see you use IDE so it is much easier to use comment tokens for example TODO. It is transparent to the compilers, does not mess in the source code and IMO is more convenient. You have some predefined tokens and you can create your own ones.
warnings should be left for warnings
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
the role of #ifdef and #ifndef
Does
#ifndef _WIN32
instruct the cpp to omit the code for 32 bit windows platform ?
#ifndef _WIN32 tells the pre-processor to include the code below it till a corresponding #endif, if _WIN32 is not defined.
#ifndef _WIN32
#define STR1 "Some String"
#endif
The macro STR1 will get included if _WIN32 is not defined and will not get included if _WIN32 is defined. Please note that _WIN32 is a system defined macro. Generally, the code which is not meant for Windows platform or which is generic and cannot be compiled in Windows is placed under such #ifndef _WIN32 macros.
The MSDN page says _WIN32 will be defined by default for all 32 bit and 64 bit builds.
This directive means "don't include this code when _WIN32 macro defined".
If you define macro _WIN32 only when compile for the Win32 then this code "instruct the cpp to omit the code for 32 bit windows platform".
Well it is a preprocessor directive. It is called compilation constants.
Compiler will consider the piece of code under those #ifndef if the compilation constant(such as, _WIN32) is not defined.
I believe above explanation will help you resolving your query.
To be more specific,
#ifndef _WIN32
...
...
...
some code
...
...
...
#endif
here if you have not defined _WIN32 (such as #define _WIN32) then the code within that #if...#endif will be compiled.
hope it helps.
I would like to conditionally compile certain parts of code based on Compiler Type is there any macro for that?
Like this:
#if defined (COMPILER_TYPE e.g. GCC)
// Compile this
#elif defined (COMPILER_TYPE e.g. Visual Studio C Compiler)
// Else this
#endif
Thank you
You can check if these macros are defined, __GNUC__ for GCC and _MSC_VER for MSVC.
I came across this preprocessor definition while reading the source code in Windows Research Kernel (WRK) 1.2:
#define assert(exp) ((void) 0)
What does this code do? Why is it defined?
It defines the expression assert(anything) to do nothing.
Presumably, the environment being used does not support the ANSI C assert statement, or the programmer was unaware of the fact that it could be disabled by defining NDEBUG.
To expand on what bdonlan says, the reason the macro does not expand empty is because if it did, then something like:
assert(something) // oops, missed the semi-colon
assert(another_thing);
would compile in release mode but not in debug mode. The reason it is ((void) 0) rather than just 0 is to prevent "statement with no effect" warnings (or whatever MSVC calls them).
Just to add, this is the definition of assert in newlib too, when NDEBUG is defined as a preprocessor directive. Newlib is the open source C library that is used on Cygwin and embedded systems.
From the assert manual in newlib:
The macro is defined to permit you to turn off all uses of assert at
compile time by defining NDEBUG as a preprocessor variable. If you do this,
the assert macro expands to (void(0))