본문 바로가기
C++ 200제/코딩 IT 정보

undefined reference to sqrt 에러, gcc math 함수 사용

by vicddory 2019. 5. 24.

undefined reference to sqrt 에러, gcc math 함수 사용


▷ 에러 메시지

1
2
3
4
5
/tmp/ccqh1iCf.o: In function `main':
test.c:(.text+0x2d): undefined reference to `sqrt'
 
collect2: ld returned 1 exit status 
cs


결론부터 말씀드리면 컴파일할 때, -lm 추가하면 됩니다. 아래와 같은 코드는 정상이지만 컴파일 명령어 문제로 에러가 발생할 수 있습니다.



undefined reference to sqrt 에러, gcc math 함수 사용


▷ 예제 소스 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <math.h> 
 
int main(void)
{
    double n;
    double r;
 
    n = 1.0;
    r = sqrt(n);
 
    printf("%f\n", r);
 
    return 0;
}
cs


▷ 컴파일 명령어

1
gcc test.c -o test -lm
cs


위와 같이 -lm (마이너스 엘 엠) 붙여서 math 라이브러리 링크하면 해결 됩니다.



관련 글


AVR Studio, gcc 플러그인 에러, 컴파일 에러 (Atmega128)


컴파일러 문제 풀이, 파싱 테이블 LL 파서, 구문 분석 parser 구현


#ifndef #define 사용법이란 헤더 파일 중복 컴파일 (꼬임 방지)



ⓒ written by vicddory

undefined reference to sqrt 에러, gcc math 함수 사용

댓글