[c] 경고 : 함수의 암시 적 선언

내 컴파일러 (GCC)가 경고를합니다.

경고 : 함수의 암시 적 선언

왜 오는지 이해하도록 도와주세요.



답변

컴파일러에서 선언 ( ” prototype “)을 아직 보지 못한 함수를 사용하고 있습니다 .

예를 들면 다음과 같습니다.

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


답변

올바른 방법은 헤더에 함수 프로토 타입을 선언하는 것입니다.

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

하나의 파일 (main.c)로 대체

static int some_main(const char *name);

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


답변

main.c에서 #includes를 수행 할 때 #include 참조를 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그것을 얻을 때 그것은 또한 위반 기능을 나열해야합니다. 이 오류는 헤더 파일을 잊었거나 누락하여 발생하는 경우가 많으므로 셸 프롬프트 에서 맨 위에 섹션을 입력 man 2 functionname하고 살펴볼 수 있습니다. SYNOPSIS이 섹션에는 포함해야하는 헤더 파일이 나열됩니다. 또는 http://linux.die.net/man/을 보십시오. 이것은 하이퍼 링크로 연결되어 있고 검색하기 쉬운 온라인 매뉴얼 페이지입니다. 필요한 헤더 파일을 포함하여 헤더 파일에 함수가 정의되는 경우가 종종 있습니다. cnicutar가 말했듯이

컴파일러에서 아직 선언 ( “prototype”)을 보지 못한 함수를 사용하고 있습니다.


답변

올바른 헤더가 정의되어 있고 GlibC라이브러리 가 아닌 라이브러리 (예 : Musl C ) gcc를 사용하는 error: implicit declaration of function경우와 같은 GNU 확장 malloc_trim이 발생할 때 발생합니다.

해결책은 확장 및 헤더감싸는 것입니다 .

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


답변

함수 전에 원하는 함수를 선언해야 합니다.

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

int main(void) {

   yourfunc();
 }


답변

나는 그 질문에 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__ ...도울 수 있습니다.