mtweidmann Posted December 3, 2003 Report Share Posted December 3, 2003 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! Quote Link to comment Share on other sites More sharing options...
fuzzylizard Posted December 3, 2003 Report Share Posted December 3, 2003 (edited) 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 December 3, 2003 by fuzzylizard Quote Link to comment Share on other sites More sharing options...
mtweidmann Posted December 3, 2003 Author Report Share Posted December 3, 2003 Thanks! I've just tried compiling the same code with the ARM compiler in ADS and it worked first time. So I'm guessing its how I'm compiling the code with GCC not the code itself. Quote Link to comment Share on other sites More sharing options...
Glitz Posted December 4, 2003 Report Share Posted December 4, 2003 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.