C语言#error和#line使用方法

C语言#error和#line使用方法

一、使用方法:

       1、#error方法:

            

       2、#line方法:

           


二、代码测试:

       1、#error  

#include <stdio.h>

#ifdef __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和g++编译器试下,就知道了,这里不讲了

看下终端输出:


  2、#line

#include <stdio.h>

int main()
{   
    #line 1  "a.c"
    printf("[%s : %d] %s\n",__FILE__,__LINE__,"a.c");
    #line 1 "b.c"
    printf("[%s : %d] %s\n",__FILE__,__LINE__,"b.c");
    #line   1   "delphi_tang.c"
    return 0;
}

终端输出:

 


  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在 C 语言中,获取 cacheline 的大小需要使用特定的操作系统相关的函数或者编译器相关的宏。以下是一些常见的方法: 1. 使用 Linux 系统的 sysconf() 函数获取 cacheline 大小: ```c #include <unistd.h> #include <stdio.h> int main() { long cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); printf("cacheline size on this system is %ld bytes\n", cacheline_size); return 0; } ``` 2. 使用 GCC 编译器的 __builtin_cpu_supports() 函数获取 cacheline 大小: ```c #include <stdio.h> #include <stdint.h> #include <stdbool.h> int main() { bool has_cacheline_size = false; uint32_t size = 0; if (__builtin_cpu_supports("avx")) { size = __builtin_ia32_cpuid_count(0x80000006, 0, &eax, &ebx, &ecx, &edx); if (size && (eax & 0xff) > 0) { has_cacheline_size = true; size = (eax & 0xff) * 64; } } if (has_cacheline_size) { printf("cacheline size on this system is %u bytes\n", size); } else { printf("cannot determine cacheline size on this system\n"); } return 0; } ``` 注意:这个方法需要编译器支持 AVX 指令集。 3. 使用 Windows 系统的 GetLogicalProcessorInformation() 函数获取 cacheline 大小: ```c #include <windows.h> #include <stdio.h> int main() { PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; DWORD length = 0; BOOL result = GetLogicalProcessorInformation(buffer, &length); if (!result && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(length); result = GetLogicalProcessorInformation(buffer, &length); } if (result) { DWORD offset = 0; while (offset < length) { PSYSTEM_LOGICAL_PROCESSOR_INFORMATION info = buffer + offset / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); if (info->Relationship == RelationCache && info->Cache.Level == 1 && info->Cache.Type == CacheData) { printf("cacheline size on this system is %u bytes\n", info->Cache.LineSize); break; } offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); } } else { printf("cannot determine cacheline size on this system\n"); } free(buffer); return 0; } ``` 注意:这个方法需要在 Windows 系统上运行。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值