{ }codelper

오류 해결 사전 › C언어

C언어 implicit declaration of function

warning: implicit declaration of function ‘strlen’

컴파일러가 아직 본 적 없는 이름의 함수를 불렀다는 경고예요. C 컴파일러는 파일을 위에서 아래로 읽기 때문에, 함수를 부르는 줄보다 위에 그 함수의 선언이 있어야 해요. 선언은 #include한 헤더에 들어 있거나 내가 직접 적어요. 작은따옴표 안의 이름이 문제의 함수이니, 그 이름이 표준 함수인지, 오타인지, 내가 만든 함수인지부터 가리면 돼요.

원인 1. #include를 빠뜨렸어요

strlen의 선언은 <string.h>에 있어요. 헤더를 넣지 않으면 컴파일러는 strlen이 무엇인지 몰라요. GCC는 note: include '<string.h>'처럼 어떤 헤더를 넣으면 되는지도 알려 줘요. 아래 화면에는 경고가 두 개 보이는데, 둘 다 같은 줄의 같은 실수를 가리켜요.

이 예제는 경고만 나고 실행은 돼요. 그래도 그대로 두면 안 돼요. 선언이 없으면 컴파일러가 인수와 돌려주는 값의 타입을 확인하지 못해서, 다른 함수에서는 틀린 값이 나올 수 있어요.

틀린 코드

#include <stdio.h> int main(void) {    char s[] = "hello";    printf("%d\n", (int)strlen(s));    return 0;}
실행하면
main.c: In function ‘main’:
main.c:5:25: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]
    5 |     printf("%d\n", (int)strlen(s));
      |                         ^~~~~~
main.c:2:1: note: include ‘<string.h>’ or provide a declaration of ‘strlen’
    1 | #include <stdio.h>
  +++ |+#include <string.h>
    2 |
main.c:5:25: warning: incompatible implicit declaration of built-in function ‘strlen’ [-Wbuiltin-declaration-mismatch]
    5 |     printf("%d\n", (int)strlen(s));
      |                         ^~~~~~
main.c:5:25: note: include ‘<string.h>’ or provide a declaration of ‘strlen’
5

고쳐서 실행해 보기 →

고친 코드

그 함수가 들어 있는 헤더를 파일 맨 위에 #include해요.

#include <stdio.h>#include <string.h> int main(void) {    char s[] = "hello";    printf("%d\n", (int)strlen(s));    return 0;}
실행 결과
5

고쳐서 실행해 보기 →

자주 쓰는 함수의 헤더: printf scanf는 <stdio.h>, strlen strcpy strcmp는 <string.h>, malloc free abs atoi는 <stdlib.h>, sqrt pow는 <math.h>, isdigit toupper는 <ctype.h>예요.

원인 2. 함수 이름을 잘못 적었어요

printf를 prinft로 적으면 컴파일러에게는 처음 보는 함수예요. 비슷한 이름이 있으면 did you mean 'printf'?라고 물어봐 줘요.

이때는 경고로 끝나지 않아요. prinft라는 함수는 어디에도 없어서, 바로 뒤 링크 단계에서 undefined reference to 오류가 나고 실행 파일이 만들어지지 않아요. 그 줄의 /tmp/cc....o는 컴파일러가 잠깐 만든 파일의 이름이라 컴파일할 때마다 달라요.

틀린 코드

#include <stdio.h> int main(void) {    prinft("hello\n");    return 0;}
실행하면
main.c: In function ‘main’:
main.c:4:5: warning: implicit declaration of function ‘prinft’; did you mean ‘printf’? [-Wimplicit-function-declaration]
    4 |     prinft("hello\n");
      |     ^~~~~~
      |     printf
/usr/bin/ld: /tmp/ccRp4kfl.o: in function `main':
main.c:(.text.startup+0x10): undefined reference to `prinft'
collect2: error: ld returned 1 exit status

고쳐서 실행해 보기 →

고친 코드

이름의 철자와 대소문자를 고쳐요. C는 Printf와 printf도 다른 이름으로 봐요.

#include <stdio.h> int main(void) {    printf("hello\n");    return 0;}
실행 결과
hello

고쳐서 실행해 보기 →

원인 3. 내가 만든 함수가 main 아래에 있어요

main에서 add를 부르는 줄을 읽을 때, 컴파일러는 아직 그 아래에 있는 add를 보지 못했어요. 그래서 처음 보는 함수라고 경고해요.

틀린 코드

#include <stdio.h> int main(void) {    printf("%d\n", add(3, 4));    return 0;} int add(int a, int b) {    return a + b;}
실행하면
main.c: In function ‘main’:
main.c:4:20: warning: implicit declaration of function ‘add’ [-Wimplicit-function-declaration]
    4 |     printf("%d\n", add(3, 4));
      |                    ^~~
7

고쳐서 실행해 보기 →

고친 코드

함수 전체를 main 위로 옮기거나, main 위에 선언 한 줄(int add(int a, int b);)을 적어요. 선언은 함수의 첫 줄에 ;을 붙인 모양이에요.

#include <stdio.h> int add(int a, int b); int main(void) {    printf("%d\n", add(3, 4));    return 0;} int add(int a, int b) {    return a + b;}
실행 결과
7

고쳐서 실행해 보기 →

선언이 없으면 컴파일러는 그 함수가 int를 돌려준다고 짐작해요. 그래서 int를 돌려주는 add는 경고로 끝나지만, double을 돌려주는 함수였다면 짐작과 달라서 conflicting types for 오류까지 나요.

이 오류가 났을 때 확인할 것

  1. 경고의 작은따옴표 안에 있는 함수 이름을 보세요. 철자와 대소문자가 맞나요?
  2. 표준 함수라면 바로 아래의 note: include ... 줄이 알려 주는 헤더를 #include하세요.
  3. 내가 만든 함수라면 부르는 줄보다 위에 함수나 선언이 있는지 확인하세요.
  4. 경고뿐이어서 실행이 되더라도 꼭 고치세요. GCC 14부터는 이것이 경고가 아니라 오류예요.
  5. 컴파일러마다 글이 달라요. clang은 call to undeclared function, Visual Studio는 warning C4013으로 알려 줘요.

연습 문제

더 보기