webassembly003 whisper.cpp的main项目-4 一些宏定义细节

标记函数为已弃用宏:WHISPER_DEPRECATED

  • 对于 GNU 编译器(__GNUC__),使用 __attribute__((deprecated(hint))) 属性将函数标记为已弃用,并附带指定的提示信息。
  • 对于微软 Visual C++ 编译器(_MSC_VER),使用 __declspec(deprecated(hint)) 属性实现相同的目的。
  • 对于其他编译器,函数不会被标记为已弃用。
cpp 复制代码
// 检查是否为 GNU 编译器
#ifdef __GNUC__
    // 对于 GNU 编译器,使用 __attribute__((deprecated(hint))) 将函数标记为已弃用
    // hint 参数用于提供关于已弃用的提示信息
    #define WHISPER_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
// 检查是否为 Microsoft Visual C++ 编译器
#elif defined(_MSC_VER)
    // 对于 Microsoft Visual C++ 编译器,使用 __declspec(deprecated(hint)) 将函数标记为已弃用
    // hint 参数用于提供关于已弃用的提示信息
    #define WHISPER_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
// 其他编译器
#else
    // 对于其他编译器,不进行已弃用标记
    #define WHISPER_DEPRECATED(func, hint) func
#endif

使用示例

  • whisper_init_from_file_no_state 函数标记为已弃用,并建议使用 whisper_init_from_file_with_params_no_state 函数代替。

    复制代码
      WHISPER_DEPRECATED(
          WHISPER_API struct whisper_context * whisper_init_from_file_no_state(const char * path_model),
          "use whisper_init_from_file_with_params_no_state instead"
      );

WHISPER_API

cpp 复制代码
// 如果定义了 WHISPER_SHARED
#ifdef WHISPER_SHARED
    // 如果目标平台为 Windows(_WIN32)
    #ifdef _WIN32
        // 如果是构建共享库(WHISPER_BUILD 已定义)
        #ifdef WHISPER_BUILD
            // 定义 WHISPER_API 为 __declspec(dllexport)(导出符号)
            #define WHISPER_API __declspec(dllexport)
        // 如果是使用共享库(WHISPER_BUILD 未定义)
        #else
            // 定义 WHISPER_API 为 __declspec(dllimport)(导入符号)
            #define WHISPER_API __declspec(dllimport)
        #endif
    // 如果目标平台不是 Windows
    #else
        // 定义 WHISPER_API 为 __attribute__ ((visibility ("default")))(设置可见性为默认)
        #define WHISPER_API __attribute__ ((visibility ("default")))
    #endif
// 如果未定义 WHISPER_SHARED
#else
    // 定义 WHISPER_API 为空
    #define WHISPER_API
#endif
  • 注:宏定义使用 #define 关键字,定义 WHISPER_API 为空时(#define WHISPER_API),由于编译器在预处理阶段会将这些宏的出现替换为相应的代码或值。定义为空即什么也不做。

__declspec(dllimport) 和 __declspec(dllexport)

__declspec(dllimport)

  1. 作用

    • 用于标记在外部 DLL 中定义的函数或变量,表示这些函数或变量将在运行时从 DLL 中导入到当前模块(通常是可执行文件或其他 DLL)中使用。
  2. 示例

    cpp 复制代码
    // 在可执行文件或其他 DLL 中使用的声明
    __declspec(dllimport) void myFunction();
    __declspec(dllimport) int myVariable;

__declspec(dllexport)

  1. 作用

    • 用于标记在当前模块中定义的函数或变量,表示这些函数或变量将在运行时导出到 DLL 中,以便其他模块可以使用。
  2. 示例

    cpp 复制代码
    // 在 DLL 中定义的导出函数和变量
    __declspec(dllexport) void myFunction() {
        // 函数体
    }
    __declspec(dllexport) int myVariable = 42;

使用示例

复制代码
WHISPER_API struct whisper_context * whisper_init_from_file(const char * path_model);
相关推荐
羊群智妍1 小时前
2026 AI搜索流量密码:免费GEO监测工具,优化效果看得见
笔记·百度·微信·facebook·新浪微博
山岚的运维笔记3 小时前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver
lpruoyu4 小时前
【Android第一行代码学习笔记】Android架构_四大组件_权限_持久化_通知_异步_服务
android·笔记·学习
wdfk_prog5 小时前
[Linux]学习笔记系列 -- [drivers][mmc][mmc_sdio]
linux·笔记·学习
果果燕5 小时前
今日学习笔记:双向链表、循环链表、栈
笔记·学习·链表
觉醒大王5 小时前
AI写的青基中了
人工智能·笔记·深度学习·学习·职场和发展·学习方法
明月醉窗台5 小时前
qt使用笔记六之 Qt Creator、Qt Widgets、Qt Quick 详细解析
开发语言·笔记·qt
Hello_Embed7 小时前
libmodbus 移植 STM32(USB 串口后端篇)
笔记·stm32·单片机·嵌入式·freertos·libmodbus
张祥6422889048 小时前
RTKLIB源码和理论结合分析笔记三
笔记
日更嵌入式的打工仔8 小时前
0欧电阻作用
笔记