| | |
Preprocessor #warning Command Question
![]() |
•
•
Join Date: Sep 2008
Posts: 90
Reputation:
Solved Threads: 12
Is there a way to #define something as a #warning?
I tried using the following code, but it gives me an error:
'warning' undeclared (first use this function)
This is the part of code where the error appears:
Any help on this please?
If it's not possible to perform, please tell
Thanks in advance
I tried using the following code, but it gives me an error:
'warning' undeclared (first use this function)
This is the part of code where the error appears:
c++ Syntax (Toggle Plain Text)
#define something #warning This is not a standard function! int main(void) { something return 0; }
Any help on this please?
If it's not possible to perform, please tell

Thanks in advance
Last edited by unbeatable0; Oct 7th, 2008 at 9:45 am.
I don't think the pre-processor is capable of expanding lines which are themselves pre-processing statements.
In short, I don't think it's going to work.
In short, I don't think it's going to work.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
--
If your code lacks code tags, you will be IGNORED
•
•
Join Date: Sep 2008
Posts: 90
Reputation:
Solved Threads: 12
Any help on this? If it helps, I want the macro to be a function. For example:
If it's not possible to do this way, is there another way to do it, which will give a warning instead of the function, in case the function cannot be used?
c++ Syntax (Toggle Plain Text)
#ifndef _WINDOWS_H #define test(); #warning <windows.h> needs to be included for this function! #endif int main(void) { test(); return 0; }
If it's not possible to do this way, is there another way to do it, which will give a warning instead of the function, in case the function cannot be used?
Last edited by unbeatable0; Oct 8th, 2008 at 3:46 am.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
Salem is right; there is no way to #define a macro that expands to a preprocessor directive.
There is also the incidental concern that #warning is not a standard preprocessor directive.
Any C++ compiler will generate an error on an attempt to call a function that has not previously been declared.
I can't see the point of what you're trying to do. If <window.h> is not included
will generate an error message that gives specific information on requirements.
Of course, if you need to use the above (eg if you have a header that relies on <windows.h>) it would be easier to #include <windows.h> anyway
There is also the incidental concern that #warning is not a standard preprocessor directive.
Any C++ compiler will generate an error on an attempt to call a function that has not previously been declared.
I can't see the point of what you're trying to do. If <window.h> is not included
C++ Syntax (Toggle Plain Text)
#ifndef _WINDOWS_H #error <windows.h> needs to be included to compile this source file #endif
Of course, if you need to use the above (eg if you have a header that relies on <windows.h>) it would be easier to #include <windows.h> anyway
> #define test(); #warning <windows.h> needs to be included for this function!
But if you don't have windows.h, then the code would have failed to compile before this point anyway.
How to tell your OS/Compiler/etc at compile-time
http://predef.sourceforge.net/
You have to do something like this
Or if you're trying to spot obsolete functionality, and you're using gcc, then perhaps this (this is the 2nd obscure snippet from the gcc manual today
)
But if you don't have windows.h, then the code would have failed to compile before this point anyway.
How to tell your OS/Compiler/etc at compile-time
http://predef.sourceforge.net/
You have to do something like this
C++ Syntax (Toggle Plain Text)
#ifdef _WIN32 #include <windows.h> #else #warning needs windows.h // in case the compiler ignores #warning int test ( ) { fprintf( stderr, "_WIN32 not set, compilation should have failed!\n" ); exit( 0 ); return 0; } #endif
Or if you're trying to spot obsolete functionality, and you're using gcc, then perhaps this (this is the 2nd obscure snippet from the gcc manual today
)•
•
•
•
Originally Posted by gcc manual
deprecated
The deprecated attribute results in a warning if the variable is used anywhere
in the source file. This is useful when identifying variables that are expected
to be removed in a future version of a program. The warning also includes the
location of the declaration of the deprecated variable, to enable users to easily
find further information about why the variable is deprecated, or what they
should do instead. Note that the warning only occurs for uses:
extern int old_var __attribute__ ((deprecated));
extern int old_var;
int new_fn () { return old_var; }
results in a warning on line 3 but not line 2.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
--
If your code lacks code tags, you will be IGNORED
![]() |
Other Threads in the C++ Forum
- Previous Thread: factoring binomials
- Next Thread: Need help with AddFontResource
Views: 1437 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete desktop display dll dynamic encryption error exception file files fstream function functions game givemetehcodez graph gui helpwithhomework homework i/o iamthwee input int lazy library linked-list linker list loop looping loops math matrix member memory network newbie news number object objects opengl output parameter path pointer pointers problem program programming project random read recursion recursive reference sort sorting spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock







