函数声明不是一个原型 [英] function declaration isn't a prototype

查看:400
本文介绍了函数声明不是一个原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我创建了一个库,

mylib.c:

#include <mylib.h>
int
testlib() {
    printf("Hello world\n");
    return (0);
}

mylib.h:

#include <stdio.h>
extern int testlib();

在我的计划中,我试图调用这个库函数:

In my program, I've attempted to call this library function:

myprogram.c:

#include <mylib.h>

int
main (int argc, char *argv[]) {
    testlib();
    return (0);
}

当我尝试编译这个节目,我得到以下错误:

When I attempt to compile this program I get the following error:

In file included from myprogram.c:1
mylib.h:2 warning: function declaration isn't a prototype

我使用:海湾合作委员会(GCC)3.4.5 20051201(红帽3.4.5-2)

我的问题是,什么是定义一个函数原型的正确方法?

My question is, what is the proper way to declare a function prototype?

推荐答案

在C INT富() INT富(无效)有不同的功能。 INT富()接受的参数任意数,而 INT富(无效) 0接受的论点。在C ++中,他们的意思是一样的。我建议你​​使用无效始终如一,当你的意思是没有参数。

In C int foo() and int foo(void) are different functions. int foo() accepts an arbitrary number of arguments, while int foo(void) accepts 0 arguments. In C++ they mean the same thing. I suggest that you use void consistently when you mean no arguments.

如果你有一个变量 A 的extern int类型的; 是一种方式来告诉<$编译器C $ C> A 是,可能是在不同的翻译单元(C编译器的源文件说)present,不解决它,直到链接时的象征。另一方面,这是函数名符号链接时被反正解决。存储类说明对功能的意义(的extern 静态)仅影响其知名度和 EXTERN 是默认的,所以的extern 其实是不必要的。

If you have a variable a, extern int a; is a way to tell the compiler that a is a symbol that might be present in a different translation unit (C compiler speak for source file), don't resolve it until link time. On the other hand, symbols which are function names are anyway resolved at link time. The meaning of a storage class specifier on a function (extern, static) only affects its visibility and extern is the default, so extern is actually unnecessary.

我建议删除的extern ,这是多余的,通常省略。

I suggest removing the extern, it is extraneous and is usually omitted.

这篇关于函数声明不是一个原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆