【问题标题】:conditional defining static variables条件定义静态变量
【发布时间】:2016-03-30 20:15:04
【问题描述】:

寻找一种方法来进行条件静态声明。 我想在我的程序中使一些变量在我的程序中在发布和全局编译时静态化,以便在调试中编译时进行测试。 因为我不希望我的所有静态变量在调试时都变成全局变量,所以我想也许我可以做一个宏来根据编译标志定义一个静态变量。我想要这样的东西:

#define STATIC(def) (def) ? : static

这样的事情可以做吗? 使用 C 标准 89

【问题讨论】:

  • 条件运算符不是运行时的吗?

标签: c static macros conditional c-preprocessor


【解决方案1】:

使用预处理器#if 怎么样?你可以这样做:

#if YOUR_CONDITION
#define STATIC(def) static def
#else
#define STATIC(def) def
#endif

您也可以使用#ifdef 和#ifndef:

#ifdef DEBUG_MODE /*Define as static when DEBUG_MODE is defined*/
#define STATIC(def) static def
#else
#define STATIC(def) def
#endif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多