Jump to content

Math.h & sin & C


Recommended Posts

I normally use C++, but for a project I'm working on at the moment I have to use C. Anyway, how do I use "sin" and "cos" in C? I've made a simple console program in Kdevelop which just fills an array with samples of a sin wave. It has "#include <math.h>" at the top. When I try to compile it i get the following message:

 

gmake all-recursive

gmake[1]: Entering directory `/home/martin/Documents/ac_test'

Making all in ac_test

gmake[2]: Entering directory `/home/martin/Documents/ac_test/ac_test'

gcc -DHAVE_CONFIG_H -I. -I. -I.. -O2 -O0 -g3 -Wall -c main.c

/bin/sh ../libtool --mode=link gcc -O2 -O0 -g3 -Wall -o ac_test main.o

gcc -O2 -O0 -g3 -Wall -o ac_test main.o

main.o(.text+0x86): In function `main':

/home/martin/Documents/ac_test/ac_test/main.c:45: undefined reference to `sin'

collect2: ld returned 1 exit status

gmake[2]: Leaving directory `/home/martin/Documents/ac_test/ac_test'

gmake[2]: *** [ac_test] Error 1

gmake[1]: *** [all-recursive] Error 1

gmake[1]: Leaving directory `/home/martin/Documents/ac_test'

gmake: *** [all-recursive-am] Error 2

*** failed ***

 

I've checked that I've got everything installed and can't spot anything obiviously wrong witht he code. HELP!

Link to comment
Share on other sites

You have to add a compiler flag in order to get the Math stuff to work. Try this

 

gcc -lm

 

and see if that will work. Most ide's have a section where you can add any flags that you need for compiling.

Edited by fuzzylizard
Link to comment
Share on other sites

Yes, under GCC you have to link with the -lm option in order to link in the math libraries. Some other linkers link these libraries in automatically. Just specifying the math.h header files with GCC is not enough.

 

Glitz.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...