警告/错误“功能声明不是原型". [英] Warning/error "function declaration isn't a prototype"

查看:47
本文介绍了警告/错误“功能声明不是原型".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建的图书馆,

I have a library I created,

#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:

#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(GCC)3.4.5 20051201(Red Hat 3.4.5-2)

声明函数原型的正确方法是什么?

What is the proper way to declare a function prototype?

推荐答案

在C中, int foo() int foo(void)是不同的功能. int foo()接受任意数量的参数,而 int foo(void)接受0个参数.在C ++中,它们的含义相同.我建议您在没有参数的情况下始终使用 void .

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 a; 是一种告诉编译器 a 是可能是出现在另一个翻译单元中(C编译器代表源文件),请在链接时间之前将其解析.另一方面,作为函数名称的符号无论如何都在链接时解析.函数( extern static )上的存储类说明符的含义仅影响其可见性,而 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天全站免登陆