在xcode中,當使用gcc編譯應用程序時,如果NSZombieEnabled之類的東西在分發版本上運行,我想拋出編譯時間錯誤,從而確保編譯失敗,不小心做了一些愚蠢的事情。如何在GCC和Xcode中手動拋出編譯器錯誤
我做了一些Google搜索,但無法弄清楚如何在符合某些條件時讓編譯器保釋。當然,它一定很容易,我只是沒有找到它?
在xcode中,當使用gcc編譯應用程序時,如果NSZombieEnabled之類的東西在分發版本上運行,我想拋出編譯時間錯誤,從而確保編譯失敗,不小心做了一些愚蠢的事情。如何在GCC和Xcode中手動拋出編譯器錯誤
我做了一些Google搜索,但無法弄清楚如何在符合某些條件時讓編譯器保釋。當然,它一定很容易,我只是沒有找到它?
#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!"
由於NSZombieEnabled
是一個環境變量,你需要做一些聰明的你構建腳本來定義宏爲0或1。
嚴格地說,#error
指令發生在C預處理器中,而不是gcc
。但在你描述的情況下,這應該不重要。
NSZombieEnabled
是一個環境標誌和,據我所知,應該不會影響到生成的二進制文件(儘管它可能影響編譯的速度。)
你可以做另一個有趣的事情是操縱你的編譯器的輸出腳本。如果您正在使用自定義構建腳本作爲構建過程的一部分,你可以這樣做:
echo "error: this build step failed!"
或者:
echo "warning: this build step could be potentially faulty"
那些會產生(分別)錯誤或警告,將顯示在構建結果窗口中打開。我已經使用了幾次,並且它的確很有用。
complile-time assert() - a.k.a。靜態斷言 - 可能會有所幫助。這裏是我的,從http://www.pixelbeat.org/programming/gcc/static_assert.html主要衍生:
/*-----------------------------------------------------------------------------
* Compile-time ASSERT(). Similar to the BOOST_STATIC_ASSERT(). And the C++0x
* static_assert(), which also has a parameter for a useless error message
* (see correction!). Our ASSERT() can be placed anywhere in the code, except:
*
* o In a twice-included header file, without a #ifndef...#endif wrapper.
* o In the middle of a structure definition (or enum definition).
* o In C89 or C90, after a statement. But you can wrap it in braces!
*
* If you want stick something in the middle of a structure definition
* you'll need to use the ugly, three-line construct #if...#error...#endif.
* And if you do do this, the pre-processor has a much more limited idea of
* what a "constant expression" is.
*
* This is a refinement of ideas from the web (www.pixebeat.org is good). It
* is shorter than BOOST. And, I believe, is better than Linus Torvald's
* suggestion for an improved BUILD_BUG_ON(). And the do{...}while(0) wrapper
* you commonly see is totally inapplicable here: it limits permissible
* locations.
*
* The web has many suggestions using arrays with a negative index. But with
* GCC, most of these do not detect a NON-CONSTANT arg (which is easy enough
* to do in error), except for the attractive 'extern int foo[expression]',
* which also gives an 'unused variable' warning (which might be fixable via
* (void)foo). GCC 4.3 apparently has a built-in static_assert(). Update:
* typedef int array[expression] seems also to be good.
*/
#define CONCAT_TOKENS(a, b) a ## b
#define EXPAND_THEN_CONCAT(a,b) CONCAT_TOKENS(a, b)
#define ASSERT(e) enum {EXPAND_THEN_CONCAT(ASSERT_line_,__LINE__) = 1/!!(e)}
但我糾正了在C '無用' 的消息我看來++ 0x中:
/*-----------------------------------------------------------------------------
* Correction!: The message in static_assert() isn't quite useless, and we've
* added it to ASSERTM(). This is needed for the case where two different
* header files happen by chance to have two ASSERT()'s on the same line, or
* likewise for a source file and a header file.
*
* We could also handle this via __COUNTER__, but this isn't supported by
* the SGI compiler (and is uglier). And we can't use __FILE__, because it
* doesn't usually expand to a valid C token (e.g. it has a dot c or dot h).
*/
#define ASSERTM(e,m) enum{EXPAND_THEN_CONCAT(m##_ASSERT_line_,__LINE__)=1/!!(e)}
一些例子:
/*-----------------------------------------------------------------------------
* Example:
*/
ASSERTM (sizeof (int16) == 2, my_global_header_h);
ASSERTM (sizeof (ord32) == 4, my_global_header_h);
ASSERTM (sizeof (int64) == 8, my_global_header_h);
/*-----------------------------------------------------------------------------
* Equally good, I believe, is the following variant, but it is slightly
* longer (and not used by us at the present time):
*/
#define ASSERTt(e) typedef int EXPAND_THEN_CONCAT(ASSERT_line_,__LINE__)[1-2*!(e)]
這似乎很難...似乎無法找到方法 – coneybeare 2010-06-08 03:07:29
@coneybeare:NSZombieEnabled是您在**運行時設置的環境變量**。在編譯時你不必擔心它。但是,如果您有編譯時調試宏,此技術很有用。 – JeremyP 2010-06-08 07:34:19
+1請注意,你也可以做'#警告這是一個警告'發出警告而不是錯誤。 – 2010-06-19 15:23:29