16 #error 和 #line

1 #error 的用法

  • #error 用于生成一个编译错误信息

  • 用法:#error 编译指示字用于自定义程序员特有的编译错误信息;类似的,#warning 用于生成编译警告,常用于条件编译的情形

    #error message
    message 不需要用双引号包围
    
  • #error 是一种预编译器指示字

  • #error 可用于提示编译条件是否满足

  • 编译过程中的任何错误信息都意味着无法生成最终的可执行程序

    #include <stdio.h>
    
    #ifndef __cplusplus
        #error This file should be processed with C++ compiler.
    #endif
    
    class CppClass
    {
    private:
        int m_value;
    public:
        CppClass() {
            
        }
        
        ~CppClass() {
        }
    };
    
    int main()
    {
        return 0;
    }
    //使用gcc编译,输出信息
    test.c:4: error: #error This file should be processed with C++ compiler.
    
  • #error 在条件编译中的应用

    #include <stdio.h>
    
    void f()
    {
    #if ( PRODUCT == 1 )
        printf("This is a low level product!
    ");
    #elif ( PRODUCT == 2 )
        printf("This is a middle level product!
    ");
    #elif ( PRODUCT == 3 )
        printf("This is a high level product!
    ");
    #else
        #error The "PRODUCTY" is NOT defined!
    #endif
    }
    
    int main()
    {
        f();
        
        printf("1. Query Information.
    ");
        printf("2. Record Information.
    ");
        printf("3. Delete Information.
    ");
    
    #if ( PRODUCT == 1 )
        printf("4. Exit.
    ");
    #elif ( PRODUCT == 2 )
        printf("4. High Level Query.
    ");
        printf("5. Exit.
    ");
    #elif ( PRODUCT == 3 )
        printf("4. High Level Query.
    ");
        printf("5. Mannul Service.
    ");
        printf("6. Exit.
    ");
    #endif
        
        return 0;
    }
    
    //gcc: gcc -DPRODUCT=2 test.c
    This is a middle level product!
    1. Query Information.
    2. Record Information.
    3. Delete Information.
    4. High Level Query.
    5. Exit.
    
    //gcc: gcc -DPRODUCT=1 test.c
    This is a low level product!
    1. Query Information.
    2. Record Information.
    3. Delete Information.
    4. Exit.
    
    //gcc: gcc -DPRODUCT=3 test.c
    This is a high level product!
    1. Query Information.
    2. Record Information.
    3. Delete Information.
    4. High Level Query.
    5. Mannul Service.
    6. Exit.
    
    //gcc: gcc test.c
    test.c: In function 'f'
    test.c:12: error: #error The "PRODUCTY" is NOT defined!
    

2 #line 的用法

  • #line 用于强制指定新的行号编译文件名,并对源程序的代码重新编号

  • 用法:#line 编译指示字的本质是重定义 __LINE____FILE__

    #line number filename
    filename 可省略
    
  • #line 的使用

    #include <stdio.h>
    
    // The code section is written by A.
    // Begin
    #line 1 "a.c"
    
    // End
    
    // The code section is written by B.
    // Begin
    #line 1 "b.c"
    
    // End
    
    // The code section is written by C.
    // Begin
    #line 1 "main.c"
    
    
    int main()
    {
        printf("%s : %d
    ", __FILE__, __LINE__);
        
        printf("%s : %d
    ", __FILE__, __LINE__);
        
        return 0;
    }
    
    // End
    
原文地址:https://www.cnblogs.com/bky-hbq/p/13618829.html