Results 1 to 2 of 2

Thread: compiation of sqrt funtion in C

  1. #1
    Join Date
    Jun 2012
    Beans
    6

    Exclamation compiation of sqrt funtion in C

    i have written a program,
    were i have to find a square root of a function.
    but i get the following error while compilation.

    dharmesh@dharmesh-ThinkPad-SL400:~/bg/ch5$ gcc -Wall p10.c -o p10.o
    /tmp/cckBRHAS.o: In function `main':
    p10.c:(.text+0xf0): undefined reference to `sqrt'
    p10.c:(.text+0x134): undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    dharmesh@dharmesh-ThinkPad-SL400:~/bg/ch5$

    i have used the header file math.h, and can't find any error in my simple program,
    kindly give a sol to my problem!
    Last edited by dharmes; June 10th, 2012 at 12:31 PM. Reason: typing mistake

  2. #2
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: compiation of sqrt funtion in C

    Probably you need to add -lm for the linker. Say that your file sqrt.c contains the following code
    Code:
    /* Square root of a number using Built-In Function */
    
    #include <stdio.h>
    #include <math.h>
    
    void main()
    {
     double n,r;
     double sqrt(double n);
     printf("Enter the no.: ");
     scanf("%lf",&n);
     r=sqrt(n);
     printf("The root of %6.4lf is %6.4lf",n,r);
    }
    Then you can compile and link it with the following command line
    Code:
    cc -o sqrt sqrt.c -lm

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •