簡體   English   中英

警告:function 的隱式聲明

[英]warning: implicit declaration of function

我的編譯器(GCC)給了我警告:

警告:function 的隱式聲明

請幫助我理解為什么會這樣。

您正在使用編譯器尚未看到聲明(“原型”)的函數。

例如:

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);

正確的方法是在頭文件中聲明函數原型。

例子

主文件

#ifndef MAIN_H
#define MAIN_H

int some_main(const char *name);

#endif

主文件

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

在 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 說的,

您正在使用編譯器尚未看到聲明(“原型”)的函數。

您需要在函數之前聲明所需的函數:

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

int main(void) {

   yourfunc();
 }

如果你有正確的頭文件中定義和使用的是非GlibC庫(如MUSLÇgcc也將拋出error: implicit declaration of function時GNU擴展,如malloc_trim遇到。

解決方案是包裝擴展和標題

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

不要忘記,如果在您的函數中調用的任何函數及其原型必須位於代碼中您的函數之上,否則編譯器在嘗試編譯您的函數之前可能找不到它們。 這將產生有問題的錯誤。

出現此錯誤是因為您嘗試使用編譯器不理解的函數。 如果您嘗試使用的函數是用 C 語言預定義的,則只需包含與隱式函數關聯的頭文件。 如果它不是預定義的函數,那么在主函數之前聲明函數總是一個好習慣。

GNUC 編譯器告訴您它可以在程序 scope 中找到特定的 function 名稱。 嘗試在 header 文件上將其定義為私有原型函數,然后將其導入主文件

我認為這個問題不是 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

由於__typeof__()嘗試使用__typeof__()代替。 另外gcc ... -Dtypeof=__typeof__ ...可以提供幫助。

我有一個遺留代碼,我需要在編譯時保留所有警告。 是的,是的,我知道如何正確編碼,我的新代碼沒有這樣的錯誤,我修復了所有這些錯誤,但這個代碼部分不是我的,我不想重寫它,因為我只需要編譯很多工作它沒有錯誤。 它在 linux 上編譯“很好”(只有警告)我只需要在 mac 上禁用它。 我知道我過去在某個地方禁用了它......但我忘記了在哪里。 如何禁用它?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM