C Preprocessor Throw Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring c preprocessor error directive developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask c preprocessor error macro Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join c preprocessor define them; it only takes a minute: Sign up Generating a preprocessor error if a define is used up vote 0 down vote favorite Is it possible with the C++ preprocessor to emit an #error if a particular #define is c preprocessor if used? Something like this: #define this_must_not_be_used #error You shouldn't use that. c++ c c-preprocessor share|improve this question edited Jan 16 '15 at 9:58 Mat 135k21234273 asked Jan 16 '15 at 9:56 Timmmm 21k19131147 1 #ifdef this_must_not_be_used #error You shouldn't use that. #endif? –How Chen Jan 16 '15 at 9:59 Yes and no... you can't define a macro that includes a pre-processor directive (#error), but if the goal is to trap programmers who accidentally (or otherwise) use
C Preprocessor Tutorial
this_must_not_be_used in their code, then the above will "work" because the compiler will throw an error anyway! –TripeHound Jan 16 '15 at 10:00 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted There is no standard way of defining a macro in such a way that its use, wherever it is, will give you a compilation error, especially not one that gives a clear and useful error message. For all you know, the code that uses it might just be stringizing its result, or it might be part of an assert condition that gets removed by the preprocessor. For most practical purposes, putting something that cannot possibly be part of a valid C (or C++) program will be good enough. Some implementations do have implementation-specific methods of achieving exactly what you ask for, though. For instance, with GCC, you can use #pragma GCC poison this_should_not_be_used where its subsequent use, no matter how it ends up used, will give: error: attempt to use poisoned "this_should_not_be_used" You may want to look at your own compiler's documentation to see if it has anything similar. You can also use conditional macro definitions, so that with GCC you use this approach, with your compiler you use your compiler's approach, and with an unknown compiler you fall back to the standard method of providing a macro definition that will probably lead to a difficult-to-read
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers
C Preprocessor Ifdef
Retired content Samples We’re sorry. The content you requested has been removed. You’ll c preprocessor ## operator be auto redirected in 1 second. C/C++ Preprocessor Reference Preprocessor Preprocessor Directives Preprocessor Directives #error Directive #error Directive #error Directive c preprocessor concatenate #define Directive #error Directive #if, #elif, #else, and #endif Directives #ifdef and #ifndef Directives #import Directive #include Directive #line Directive Null Directive #undef Directive #using Directive TOC Collapse the table of content http://stackoverflow.com/questions/27981327/generating-a-preprocessor-error-if-a-define-is-used Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. #error Directive (C/C++) Visual Studio 2015 Other Versions Visual Studio 2013 Visual Studio 2012 Visual Studio 2010 Visual Studio 2008 Visual Studio 2005 Visual Studio .NET 2003 The #error directive emits a user-specified error message at compile time and then terminates https://msdn.microsoft.com/en-us/library/c8tk0xsk.aspx the compilation.Syntax Copy #errortoken-string RemarksThe error message that this directive emits includes the token-string parameter. The token-string parameter is not subject to macro expansion. This directive is most useful during preprocessing for notifying the developer of a program inconsistency or the violation of a constraint. The following example demonstrates error processing during preprocessing: Copy #if !defined(__cplusplus) #error C++ compiler required. #endif See AlsoPreprocessor Directives Show: Inherited Protected Print Export (0) Print Export (0) Share IN THIS ARTICLE Is this page helpful? Yes No Additional feedback? 1500 characters remaining Submit Skip this Thank you! We appreciate your feedback. Dev centers Windows Office Visual Studio Microsoft Azure More... Learning resources Microsoft Virtual Academy Channel 9 MSDN Magazine Community Forums Blogs Codeplex Support Self support Programs BizSpark (for startups) Microsoft Imagine (for students) United States (English) Newsletter Privacy & cookies Terms of use Trademarks © 2016 Microsoft © 2016 Microsoft
the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs. Preprocessor http://tigcc.ticalc.org/doc/cpp.html Overview Header Files Macros Conditionals Pragmas Other Directives User-defined Diagnostics Line Control Preprocessor Output https://en.wikipedia.org/wiki/C_preprocessor C Preprocessor Command-Line Options Traditional Mode Implementation Details History GNU General Public License GNU Free Documentation License Funding Free Software Original author: Free Software Foundation, Inc. Authors of the modifications: Zeljko Juric, Sebastian Reichelt, and Kevin Kofler Published by the TIGCC Team. See the History section for details and copyright information. Permission is granted to copy, c preprocessor distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation. A copy of the license is included in the section entitled "GNU Free Documentation License". This manual contains no Invariant Sections. The Front-Cover Texts are (a) (see below), and the Back-Cover Texts are (b) (see below). (a) The FSF's Front-Cover Text is: A GNU c preprocessor error Manual (b) The FSF's Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development. Preprocessor Overview The C preprocessor, often known as cpp, is a macro processor that is used automatically by the C compiler to transform your program before compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs. The C preprocessor is intended to be used only with C, C++, and Objective-C source code. In the past, it has been abused as a general text processor. It will choke on input which does not obey C's lexical rules. For example, apostrophes will be interpreted as the beginning of character constants, and cause errors. Also, you cannot rely on it preserving characteristics of the input which are not significant to C-family languages. If a Makefile is preprocessed, all the hard tabs will be removed, and the Makefile will not work. Having said that, you can often get away with using cpp on things which are not C. Other Algol-ish programming languages are often safe (Pascal, Ada, etc.) So is assembly, with caution. '-traditional-cpp' mode preserves more white space, and
article is written like a manual or guidebook. Please help rewrite this article from a descriptive, neutral point of view, and remove advice or instruction. (February 2013) (Learn how and when to remove this template message) This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. Please help to improve this article by introducing more precise citations. (March 2015) (Learn how and when to remove this template message) (Learn how and when to remove this template message) The C preprocessor or cpp is the macro preprocessor for the C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control. In many C implementations, it is a separate program invoked by the compiler as the first part of translation. The language of preprocessor directives is only weakly related to the grammar of C, and so is sometimes used to process other kinds of text files. Contents 1 Phases 1.1 Including files 1.2 Conditional compilation 1.3 Macro definition and expansion 1.4 Special macros and directives 1.4.1 Token stringification 1.4.2 Token concatenation 1.5 User-defined compilation errors 2 Implementations 2.1 Compiler-specific preprocessor features 3 Other uses 4 See also 5 References 6 External links Phases[edit] Preprocessing is defined by the first four (of eight) phases of translation specified in the C Standard. Trigraph replacement: The preprocessor replaces trigraph sequences with the characters they represent. Line splicing: Physical source lines that are continued with escaped newline sequences are spliced to form logical lines. Tokenization: The preprocessor breaks the result into preprocessing tokens and whitespace. It replaces comments with whitespace. Macro expansion and directive handling: Preprocessing directive lines, including file inclusion and conditional compilation, are executed. The preprocessor simultaneously expands