Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

Why sin function doesn't work?

I'm using my personal linux laptop that I purchased a year ago to write following c program in vim editor:  The operating system is Ubuntu.
#include <stdio.h>
#include <math.h>

int main( )
{
   double x;
   float y;

   x = 45.0;                  /* 45 degrees */
   x *= (3.141593 / 180.0);

   y = sin(x);
   printf ("The result is: %f\n", y);

   return 0;
}

Open in new window

When I compile, I get the following error:
/tmp/ccDTU7GW.o: In function `main':
first.c:(.text+0x39): undefined reference to `sin'
collect2: error: ld returned 1 exit status

In /usr/include folder, there is math.h but I didn't find any sin function prototypes in this file.


ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of noci
noci

Much info can be found in the manual pages....
Man sin starts with:


SIN(3)                                                             Linux Programmer's Manual                                                             SIN(3)


NAME
       sin, sinf, sinl - sine function


SYNOPSIS
       #include <math.h>


       double sin(double x);
       float sinf(float x);
       long double sinl(long double x);


       Link with -lm.


   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
.....

Open in new window

Which tells you that you need the include file math.h   and what library to link with.
For most functions such a page exists. For those that don't have it the provider of the library will have some form of documentation.

in general if a library has been provided as: libxyz.so   then  you can specify it on the link command with -lxyz (gcc compiler with linkage included will accept those and pass them on to the linker (ld)


Avatar of naseeam

ASKER

@noci
Thank you so much for providing another great solution.  I didn't see it until now.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial