【问题标题】:C imlicit declaration of a function函数的 C 隐式声明
【发布时间】:2011-03-11 11:48:49
【问题描述】:

我在 Linux 和 gcc 4.2.3 上。

对于下面的代码部分,lp_parm_talloc_string 函数被隐式调用,然后被定义:

char *lp_parm_string(const char *servicename, const char *type, const char *option)
{
        return lp_parm_talloc_string(lp_servicenumber(servicename), type, option, NULL);
}

/* Return parametric option from a given service. Type is a part of option before ':' */
/* Parametric option has following syntax: 'Type: option = value' */
/* the returned value is talloced in lp_talloc */
char *lp_parm_talloc_string(int snum, const char *type, const char *option, const char *def)
{
        param_opt_struct *data = get_parametrics(snum, type, option);

        if (data == NULL||data->value==NULL) {
                if (def) {
                        return lp_string(def);
                } else {
                        return NULL;
                }
        }

        return lp_string(data->value);
}

对于这部分,出现以下错误:

param/loadparm.c:2236: error: conflicting types for 'lp_parm_talloc_string'
param/loadparm.c:2229: error: previous implicit declaration of 'lp_parm_talloc_string' was here

如何告诉编译器允许这种情况?

【问题讨论】:

  • 你绝对需要一个隐式声明吗?在文件开头或标题中明确定义您的函数。
  • 最简单的解决方案:把lp_parm_talloc_string before lp_parm_string.

标签: c function declaration implicit


【解决方案1】:

你需要在使用之前声明你的函数:

char *lp_parm_talloc_string(int snum, const char *type, const char *option, const char *def);

char *lp_parm_string(const char *servicename, const char *type, const char *option)
{
    return lp_parm_talloc_string(lp_servicenumber(servicename), type, option, NULL);
}

// ...and the rest of your code

或者只是更改这两个函数在源代码中出现的顺序。

【讨论】:

  • 是的,这解决了问题。但是是否有任何标志可以传递给 gcc 以在之后预先分配声明和映射?无需为更改而烦恼。
  • @whoi:我不知道具体的 gcc,但总的来说没有。
猜你喜欢
  • 2014-03-27
  • 2012-02-29
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多