hippocampe Posted October 24, 2003 Report Share Posted October 24, 2003 I'm doing a very simple program including calls to cos and sqrt functions. I get this when compiling: /home/hippocampe/tmp/ccSzTSx5.o(.text+0x31): In function `main':: undefined reference to `cos' /home/hippocampe/tmp/ccSzTSx5.o(.text+0x4d): In function `main': : undefined reference to `sqrt' collect2: ld returned 1 exit status Before you ask, I did include math.h :) The program compiles but does not build in Anjuta. It builds when I comment the lines containing these functions. My system is Mandrake 9.1. I don't know if my system is missing any packages? Thanks PS: Somehow it works when I use g++ instead of gcc at the command line. Anjuta uses gcc so I get error messages when I build from there. Anyone has an idea what is causing this? The file extension is .c Quote Link to comment Share on other sites More sharing options...
johnnyv Posted October 24, 2003 Report Share Posted October 24, 2003 when using gcc did you include the link flag for the math library ? eg) gcc src.c -lm -lm links to the math library Quote Link to comment Share on other sites More sharing options...
baudolino Posted October 24, 2003 Report Share Posted October 24, 2003 add the option -lm to the gcc command. look also at 14.3 in the C-faq linked below http://www.faqs.org/faqs/C-faq/faq/ baudo Quote Link to comment Share on other sites More sharing options...
hippocampe Posted October 24, 2003 Author Report Share Posted October 24, 2003 Thanks guys, that definitely solved it. I'm actually surprised I didn't come across this before.... So you actually need to explicitely link to the math library? I'm surprised that it's not included by default when you include the corresponding header. Why did it work with g++ without the -lm option? 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.