如何在 C 预处理器中生成错误或警告?

问题描述 投票:0回答:8

我有一个程序只能在 DEBUG 模式下编译。 (测试目的)

如何让预处理器阻止在 RELEASE 模式下进行编译?

c-preprocessor
8个回答
99
投票

任何地方:

#ifndef DEBUG
#error "Only Debug builds are supported"
#endif

供参考:诊断


26
投票

C 提供了一个

#error
语句,大多数编译器添加了一个
#warning
语句。 gcc 文档推荐 引用消息


20
投票

也许更复杂,但它只是以前解决方案的复制和粘贴。 :-)

#ifdef DEBUG        
    #pragma message ( "Debug configuration - OK" )
#elif RELEASE   
    #error "Release configuration - WRONG"
#else
    #error "Unknown configuration - DEFINITELY WRONG"
#endif

附言还有另一种方法可以生成警告。 创建一个未引用的标签,如

HereIsMyWarning:

并且不要引用它。在编译过程中,您会收到类似

的警告
 1>..\Example.c(71) : warning C4102: 'HereIsMyWarning' : unreferenced label

5
投票

您可以为此使用

error
指令。如果未定义
DEBUG
,以下代码将在编译时抛出错误:

#ifndef DEBUG
#error This is an error message
#endif

3
投票

如果你只是想报错:

#ifdef RELEASE
  #error Release mode not allowed
#endif

适用于大多数编译器。


3
投票

对于 GCC 和 Clang(可能还有任何支持 _Pragma 特性的编译器)你可以定义一个宏:

#if ! DEBUG
#define FIX_FOR_RELEASE(statement) _Pragma ("GCC error \"Must be fixed for release version\"")
#else
#define FIX_FOR_RELEASE(statement) statement
#endif

您可以将此宏用于临时 hack,例如绕过同事尚未编写的代码,以确保您在向公众发布构建后不会忘记修复它。要么

FIX_FOR_RELEASE()
// Code that must be removed or fixed before you can release

FIX_FOR_RELEASE(statement that must be removed or fixed before you can release);

1
投票

在Code::Blocks中,如果不需要Release模式,可以删除Release模式。为此,单击“项目”菜单,选择“属性...”,然后在“构建目标”选项卡中单击“发布”,然后单击“删除”按钮。删除 Release 模式只对当前项目有效,所以你仍然可以在其他项目中使用它。

否则,如果你真的想使用预处理器,你可以这样做:

#ifdef RELEASE
#error "You have to use the Debug mode"
#endif

0
投票

检查编译器支持

以下是对所有先前答案的扩展,关于如何检查编译器是否支持

#error
#warning
#pragma
宏。

main.c

#include "precompiler.h"

#if PRECOMPILER_MACRO_WARNING == 1
    #warning this is my warnig !
#endif

precompiler.h

#define PRECOMPILER_MACRO_WARNING   = 0
#define PRECOMPILER_MACRO_ERROR     = 0
#define PRECOMPILER_MACRO_PRAGMA    = 0
    
/* 
        #warning
        GCC: since 2.5.8
        CLANG: all (since LLVM 2.6)
        MSVC: since 7.0 (1300)
*/
#if defined __clang__ \
||  defined __GNUC__ && ( __GNUC__ > 2 || __GNUC__ == 2 && defined __GNUC_MINOR__ && __GNUC_MINOR >= 5 && defined __GNUC_PATCHLEVEL__ && __GNUC_PATCHLEVEL__ >= 8) \
||  defined _MSC_VER && _MSC_VER > 1300
        
    #undef  PRECOMPILER_MACRO_WARNING
    #define PRECOMPILER_MACRO_WARNING = 1
    
#endif
    
/* 
        #error
        GCC: since 2.5.5
        CLANG: all (since LLVM 2.6)
        MSVC: since 7.0 (1300)
*/
#if defined __clang__ \
||  defined __GNUC__ && ( __GNUC__ > 2 || __GNUC__ == 2 && defined __GNUC_MINOR__ && __GNUC_MINOR >= 5 && defined __GNUC_PATCHLEVEL__ && __GNUC_PATCHLEVEL__ >= 5) \
||  defined _MSC_VER && _MSC_VER > 1300
        
    #undef  PRECOMPILER_MACRO_ERROR
    #define PRECOMPILER_MACRO_ERROR = 1
    
#endif
    
/* 
        #pragma
        GCC: since 2.5.0
        CLANG: all (since LLVM 2.6)
        MSVC: since 14.0 (1900)
*/
#if defined __clang__ \
||  defined __GNUC__ && ( __GNUC__ > 2 || __GNUC__ == 2 && defined __GNUC_MINOR__ && __GNUC_MINOR >= 5 && defined __GNUC_PATCHLEVEL__ && __GNUC_PATCHLEVEL__ >= 0) \
||  defined _MSC_VER && _MSC_VER > 1900
        
    #undef  PRECOMPILER_MACRO_PRAGMA
    #define PRECOMPILER_MACRO_PRAGMA = 1
    
#endif
© www.soinside.com 2019 - 2024. All rights reserved.