我的编译器(GCC)给我警告:

警告:函数的隐式声明

它为什么会来?


当前回答

你正在使用一个编译器还没有看到声明(“原型”)的函数。

例如:

int main()
{
    fun(2, "21"); /* The compiler has not seen the declaration. */       
    return 0;
}

int fun(int x, char *p)
{
    /* ... */
}

你需要在main之前声明你的函数,像这样,直接或在头文件中声明:

int fun(int x, char *p);

其他回答

你正在使用一个编译器还没有看到声明(“原型”)的函数。

例如:

int main()
{
    fun(2, "21"); /* The compiler has not seen the declaration. */       
    return 0;
}

int fun(int x, char *p)
{
    /* ... */
}

你需要在main之前声明你的函数,像这样,直接或在头文件中声明:

int fun(int x, char *p);

GNU C编译器告诉您,它可以在程序作用域中找到特定的函数名。尝试在头文件中将其定义为私有原型函数,然后将其导入到主文件中。

正确的方法是在头文件中声明函数原型。

例子

main.h

#ifndef MAIN_H
#define MAIN_H

int some_main(const char *name);

#endif

c

#include "main.h"

int main()
{
    some_main("Hello, World\n");
}

int some_main(const char *name)
{
    printf("%s", name);
}

用一个文件替代(main.c)

static int some_main(const char *name);

int some_main(const char *name)
{
    // do something
}

你需要在主函数之前声明你想要的函数:

#include <stdio.h>
int yourfunc(void);

int main(void) {

   yourfunc();
 }

如果你定义了正确的头文件并且使用了非GlibC库(比如Musl C),当遇到malloc_trim这样的GNU扩展时,gcc也会抛出error:函数的隐式声明。

解决方案是包装扩展和头:

#if defined (__GLIBC__)
  malloc_trim(0);
#endif