【问题标题】:C Function is deprecatedC 函数已弃用
【发布时间】:2017-05-05 06:06:59
【问题描述】:

我使用了以下链接中的代码:

Readline Library

我定义了一个这样的结构

typedef struct {
  char *name;           /* User printable name of the function. */
  Function *func;       /* Function to call to do the job. */
  char *doc;            /* Documentation for this function.  */
} COMMAND;

当我编译代码时,编译器会显示以下警告:

“函数已弃用 [-Wdeprecated-declarations]”

那么如果不能使用函数类型应该改什么类型呢?

【问题讨论】:

  • 你介意创建一个MCVE吗?
  • 您得到的完全准确的错误是什么
  • 看警告信息,它也会告诉你哪个函数被弃用了

标签: c compiler-warnings readline


【解决方案1】:

Functiontypedef(指向返回 int 的函数的指针的别名),被 library 标记为已弃用:

typedef int Function () __attribute__ ((deprecated));

只需使用:

typedef struct {
  char *name;            /* User printable name of the function. */
  int (*func)();         /* Function to call to do the job. */
  char *doc;             /* Documentation for this function.  */
} COMMAND;

【讨论】:

    【解决方案2】:

    我认为你应该弃用 Function.并且以下可以工作。

    #include <stdio.h>
    
    typedef void (*Function)();
    
    typedef struct {
        char *name;           /* User printable name of the function. */
        Function *func;       /* Function to call to do the job. */
        char *doc;            /* Documentation for this function.  */
    }COMMAND;
    
    int main() {
        COMMAND commond;
    
        puts( "end" );
    
    
    
        return 0;
    }
    

    【讨论】:

    • 不是我的反对票,但是……这个建议的问题在于代码使用(并且需要使用)一个包含typedef 的标头用于Function,但它被注释为“弃用”(表示不使用)。如果包含其他标头,您的代码将无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多