我的编译器(GCC)给了我警告:
警告:隐式声明函数
请帮我理解为什么会这样 .
我认为问题不是100%回答 . 我正在寻找缺少typeof()的问题,这是编译时指令 .
以下链接将阐明情况:
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Typeof.html
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Alternate-Keywords.html#Alternate-Keywords
作为conculsion尝试使用 __typeof__() 而不是 . 另外 gcc ... -Dtypeof=__typeof__ ... 可以提供帮助 .
__typeof__()
gcc ... -Dtypeof=__typeof__ ...
您正在使用编译器尚未看到声明(“原型”)的函数 .
例如:
int main() { fun(2, "21"); /* The compiler has not seen the declaration. */ return 0; } int fun(int x, char *p) { /* ... */ }
你需要在main之前声明你的函数,比如直接或在 Headers 中:
int fun(int x, char *p);
正确的方法是在头文件中声明函数原型 .
main.h
#ifndef MAIN_H #define MAIN_H int some_main(const char *name); #endif
main.c
#include "main.h" int main() { some_main("Hello, World\n"); } int some_main(const char *name) { printf("%s", name); }
Alternative with one file (main.c)
static int some_main(const char *name); int some_main(const char *name) { // do something }
在main.c中执行#includes时,将#include引用放在包含引用函数的文件的顶部 . 例如假设这是main.c,你引用的函数在“SSD1306_LCD.h”中
#include "SSD1306_LCD.h" #include "system.h" #include <stdio.h> #include <stdlib.h> #include <xc.h> #include <string.h> #include <math.h> #include <libpic30.h> // http://microchip.wikidot.com/faq:74 #include <stdint.h> #include <stdbool.h> #include "GenericTypeDefs.h" // This has the 'BYTE' type definition
以上内容不会产生“隐含的功能声明”错误,但在下面将 -
#include "system.h" #include <stdio.h> #include <stdlib.h> #include <xc.h> #include <string.h> #include <math.h> #include <libpic30.h> // http://microchip.wikidot.com/faq:74 #include <stdint.h> #include <stdbool.h> #include "GenericTypeDefs.h" // This has the 'BYTE' type definition #include "SSD1306_LCD.h"
完全相同的#include列表,只是不同的顺序 .
嗯,它对我有用 .
当你得到 error: implicit declaration of function 时,它也应列出有问题的功能 . 通常由于忘记或丢失头文件而发生此错误,因此在shell提示符下您可以键入 man 2 functionname 并查看顶部的 SYNOPSIS 部分,因为此部分将列出需要包含的任何头文件 . 或者尝试http://linux.die.net/man/这是在线手册页,它们是超链接并且易于搜索 . 函数通常在头文件中定义,包括任何所需的头文件通常都是答案 . 像cnicutar说,
error: implicit declaration of function
man 2 functionname
SYNOPSIS
如果您定义了正确的标头并且正在使用非 GlibC 库(例如Musl C),那么当遇到诸如 malloc_trim 的GNU扩展时, gcc 也会抛出 error: implicit declaration of function .
GlibC
malloc_trim
gcc
解决方案是wrap the extension & the header:
#if defined (__GLIBC__) malloc_trim(0); #endif
6 回答
我认为问题不是100%回答 . 我正在寻找缺少typeof()的问题,这是编译时指令 .
以下链接将阐明情况:
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Typeof.html
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Alternate-Keywords.html#Alternate-Keywords
作为conculsion尝试使用
__typeof__()
而不是 . 另外gcc ... -Dtypeof=__typeof__ ...
可以提供帮助 .您正在使用编译器尚未看到声明(“原型”)的函数 .
例如:
你需要在main之前声明你的函数,比如直接或在 Headers 中:
正确的方法是在头文件中声明函数原型 .
示例
main.h
main.c
Alternative with one file (main.c)
在main.c中执行#includes时,将#include引用放在包含引用函数的文件的顶部 . 例如假设这是main.c,你引用的函数在“SSD1306_LCD.h”中
以上内容不会产生“隐含的功能声明”错误,但在下面将 -
完全相同的#include列表,只是不同的顺序 .
嗯,它对我有用 .
当你得到
error: implicit declaration of function
时,它也应列出有问题的功能 . 通常由于忘记或丢失头文件而发生此错误,因此在shell提示符下您可以键入man 2 functionname
并查看顶部的SYNOPSIS
部分,因为此部分将列出需要包含的任何头文件 . 或者尝试http://linux.die.net/man/这是在线手册页,它们是超链接并且易于搜索 . 函数通常在头文件中定义,包括任何所需的头文件通常都是答案 . 像cnicutar说,如果您定义了正确的标头并且正在使用非
GlibC
库(例如Musl C),那么当遇到诸如malloc_trim
的GNU扩展时,gcc
也会抛出error: implicit declaration of function
.解决方案是wrap the extension & the header: