C语言 警告:函数的隐式声明在 C99 中无效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33110645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:10:19  来源:igfitidea点击:

warning: implicit declaration of function is invalid in C99?

c

提问by Charana

This is a header file

这是一个头文件

#include <stdio.h>

int m = 18;
int x = 4;

int singles (n) {
    if (n == 1)
         return 0;
    return doubles(n-1);
} 

int doubles (n) {
    if (n == 1)
        return 0;

    return triples(n-1);
}

int triples (n) {
    if (n == 1)
        return m;

    return (singles(n-1) + doubles (n-1) + triples (n-1))*(m-1);
}

and this is the main file

这是主文件

#include <stdio.h>
#include "test.h"

int main () {
    printf("%d",singles (x));
}

So this is pretty complicated for me at-least.The idea is that in the main function i will call singles(x) where x =4 so its more like singles (4),it will call doubles (3),that will call triples (2),that will call all of singles(1) which will return 0,doubles (1) that returns 0 and triples (1) that will return m.

所以这对我来说至少是相当复杂的。这个想法是在主函数中我将调用单打(x),其中 x =4 所以它更像是单打(4),它会调用双打(3),这将调用三元组 (2) 将调用所有将返回 0 的单打 (1),返回 0 的双打 (1) 和将返回 m 的三元组 (1)。

So the error i am getting is

所以我得到的错误是

./test.h:13:12: warning: implicit declaration of function 'doubles' is invalid
      in C99 [-Wimplicit-function-declaration]
    return doubles(n-1);
       ^
./test.h:20:12: warning: implicit declaration of function 'triples' is invalid
      in C99 [-Wimplicit-function-declaration]
    return triples(n-1);
       ^
2 warnings generated. 

I tried to create a header file .h with the first script and then made a second .c script that i try to compile that won't work.I tried importing the header to try to avoid this error but it doesn't seem to work. Thanks a lot

我尝试使用第一个脚本创建一个头文件 .h,然后创建了第二个 .c 脚本,我尝试编译该脚本不起作用。我尝试导入头文件以避免此错误,但似乎没有工作。非常感谢

回答by dbush

Inside of singles, you're using doublesbefore it's defined. Similarly in doubles, you're using triplesbefore it's defined. That's why you're getting the implicit declaration errors.

在 内部singles,您在doubles定义之前使用。同样在 中doubles,您在triples定义之前使用。这就是为什么您会收到隐式声明错误的原因。

Also, you're not defining the type of the nparameter to any of these functions.

此外,您没有n为这些函数中的任何一个定义参数的类型。

You need to specify function prototypes, which declare the function without defining it:

您需要指定函数原型,它声明函数而不定义它:

int singles(int n);
int doubles(int n);
int triples(int n);

Also, you shouldn't define functions in a header file. If you include this header in multiple .c files and then link them together, you'll get an error because you'll have multiple definitions of those functions.

此外,您不应在头文件中定义函数。如果您在多个 .c 文件中包含此标头,然后将它们链接在一起,则会出现错误,因为您将有这些函数的多个定义。

Take all of the function definitions and put them in test.c. Then in test.h, put only the prototypes above. Then you can compile everything as follows:

获取所有函数定义并将它们放在 test.c 中。然后在 test.h 中,只放上面的原型。然后您可以按如下方式编译所有内容:

gcc -c test.c
gcc -c main.c
gcc -o main main.o test.o

Or in a single line:

或者在一行中:

gcc -o main test.c main.c

回答by Thomas Matthews

The term "implicit declaration" in an error message is usually generated when the compiler sees the implementation ofa call to a function before the declaration (prototype).

错误消息中的术语“隐式声明”通常是在编译器在声明(原型)之前看到函数调用的实现时生成的。

For example, you should have:
header file

例如,你应该有:
头文件

int singles(int x);
int doubles(int x);
int triples(int x);

In your source file:

在您的源文件中:

#include "header_file.h"

Also, in your function definitions (implementations) you need to specify the type of the argument:

此外,在您的函数定义(实现)中,您需要指定参数的类型:

int singles (/* data type */ parameter_name)
{
 //...
}

Edit 1: Changed implementation to function call per @John Bollinger.

编辑 1:根据@John Bollinger 将实现更改为函数调用。