PDA

View Full Version : can't compile math program with cosine algorithm in x64



t.s
December 17th, 2007, 04:17 AM
Hi all,

I've tried to make a simple program with cosine algorithm in openSUSE 10.3. Same result with kubuntu gutsy. Both are 64 bit edition.


#include <stdio.h>
#include <math.h>

main() {
printf("Cosine from PI/1 is %f\n", cos(3.1415/1));
}

when I compile that code, it output:

/tmp/ccCcCz7R.o: In function `main':
cos.c: (.text+0x1c): undefined reference to `cos'
collect2: ld returned 1 exit status

what I know is all the math function now stored in /usr/include/bits/mathcalls.h; but it's said that I cannot use (include) it directly, rather use math.h for that. I've tried both method (include math.h and include mathcalls.h) with same result, FAIL!

Distro bugs or gnu C bug?
Any suggestion?

thanks,
t.s

AlexThomson_NZ
December 17th, 2007, 04:29 AM
try

gcc -lm test.c

AlexThomson_NZ
December 17th, 2007, 04:33 AM
I should probably explain further- including the header file is not enough- you need to actually link against the math library also (-lm = link math)

xtacocorex
December 17th, 2007, 05:52 AM
I know it's along shot, but if you can't get the cosine function to work, you could always write your own algorithm using a Taylor series expansion. http://www.mathreference.com/ca,tfn.html

Majorix
December 17th, 2007, 06:32 AM
Distro bugs or gnu C bug?

Neither, it's not a bug, you are doing it wrong. You haven't linked in the math library with the -lm option passed to gcc, as stated. That's why as a beginner you should use an IDE, which does all of the compiling etc for you.

maleficent
December 17th, 2007, 07:34 AM
Neither, it's not a bug, you are doing it wrong. You haven't linked in the math library with the -lm option passed to gcc, as stated. That's why as a beginner you should use an IDE, which does all of the compiling etc for you.

I think IDEs are a matter of personal preference, beginner or not.

If you decide to persevere with command line compilation though, it might be an idea to RTM: http://gcc.gnu.org/onlinedocs/ ;)

xtacocorex
December 17th, 2007, 01:38 PM
I don't think I've never had to link using C++ if I've included math.h, but that could be C++. I take it with C you have to.

But I can agree on learning the command line options; I finally was able to compile a 3D file viewer code Wybiral and I worked (in Linux) in OS X and the only difference was changing 3 lines across 2 files and the options for the compiler.

Learning your tools is a lot more rewarding that using something to do it for you and blindly not understanding why things have to be done.