Gcc Directives 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 #error preprocessor directive in c Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation
#error C++
Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just use of #error directive in c like you, helping each other. Join them; it only takes a minute: Sign up How to manually throw a compiler error in GCC and Xcode up vote 1 down vote favorite In xcode, while compiling apps with #error in c example gcc, I want to throw compilation time errors if things like NSZombieEnabled is on for a distribution release, thus ensuring that compilation will fail and I won't accidentally do something stupid. I did some googling, but could not figure out how to cause the compiler to bail if a certain condition is met. Surely it must be easy, am I just not finding it? iphone objective-c xcode gcc compiler-construction share|improve this question asked Jun 8
Error Directive Must Use C++ For The Type Iostream
'10 at 2:44 coneybeare 27.3k13108168 add a comment| 4 Answers 4 active oldest votes up vote 10 down vote accepted Use the #error directive: #if SHOULD_FAIL #error "bad compiler!" #endif int main() { return 0; } $ gcc a.c -DSHOULD_FAIL=0 # passes fine $ gcc a.c -DSHOULD_FAIL=1 a.c:2:2: error: #error "bad compiler!" Since NSZombieEnabled is an environment variable, you'll need to do something clever in your build script to define your macro as zero or one. Strictly speaking, the #error directive occurs in the C Preprocessor, not gcc. But that shouldn't matter in the case you've described. share|improve this answer edited Jun 8 '10 at 2:52 answered Jun 8 '10 at 2:46 Mark Rushakoff 138k22294346 This seems tough… can't seem to find a way –coneybeare Jun 8 '10 at 3:07 @coneybeare: NSZombieEnabled is an environment variable that you set at run time. You don't need to worry about it at compile time. This technique is, however, useful if you have compile time debug macros. –JeremyP Jun 8 '10 at 7:34 +1 note that you can also do #warning This is a warning to issue a warning instead of an error. –Dave DeLong Jun 19 '10 at 15:23 add a comment| up vote 1 down vote NSZombieEnabled is an environment flag and, to my knowledge, should not affe
and often underused preprocessor directive. Behaviour of this preprocessor directive is the same for both C and C++ compilers. Purpose The #error directive terminates compilation and outputs the text following the directive. Format #error text All preprocessor directives begin with the # symbol. It must be the gcc #message first character on the line or the first character on the line following optional white c preprocessor message space. Some early compilers flagged an error if # was not the first character on the line. Spaces or tabs are permitted between
#warning C++
the # and error, but not escape characters or other symbols or macros. The preprocessor removes white space and concatenates the # and error together. If anything follows the #error directive (other than white space) then the program http://stackoverflow.com/questions/2994490/how-to-manually-throw-a-compiler-error-in-gcc-and-xcode is malformed. The following are valid uses: #error some error message text # error some error text to display # /* comments are white space */ error some error message to display The following are invalid uses: // #\ is not a valid preprocessor directive # \t error text to output // #" is not a valid preprocessor directive # "" text to output Use It is used to render a program malformed and output http://www.complete-concrete-concise.com/programming/c/preprocessor-%E2%80%93-the-error-directive the text following the #error directive. The text may be quoted or unquoted (it doesn't matter). No macro expansion takes place. The language specifications do not say how the text following the #error directive is to be treated. The GCC compiler, replaces all white space characters between tokens with a single white space character. I have no reason to believe other compilers behave differently since white space is not considered significant in the C and C++ languages - it serves only to seperate tokens from one another. There are many times when it is useful to halt compilation: code is incomplete code requires particular library versions code uses compiler dependent features code has specific compiler requirements Incomplete Code When developing code, it is common to create stub functions. For the final release, these stub functions need to be implemented. We can let the compiler help us catch unimplemented functions: int my_function( void ) { #error my_function not implemented return 0; } The above code will fail for every compile. It might be more useful to allow compiling during development, but break the compile when we try to compile a release version. In the following example, we assume that during development, the macro DEBUG is defined: int my_function( void ) { #ifndef DEBUG #error my_function not implemented #endif return 0; } During development, we can compil
article is written like a manual or guidebook. Please help rewrite this article from https://en.wikipedia.org/wiki/C_preprocessor a descriptive, neutral point of view, and remove advice or instruction. http://tigcc.ticalc.org/doc/cpp.html (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 in c 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, directive in c 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.
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 Overview Header Files Macros Conditionals Pragmas Other Directives User-defined Diagnostics Line Control Preprocessor Output 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, 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 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 n