By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

cos function

P: n/a
Hi - I have a problem with a simple piece of code and I can not for
the life of me work out how to fix it. The following code provides an
example:

#import <stdio.h>
#import <math.h>

int main(void)
{
cos((double) 12);
double result=cos((double) 12);
return 0;
}

Whenever I compile this, it results in the error message In function
`main':test.c:(.text+0x1b): undefined reference to `cos'

However, when I do not store or have any reference to the result (i.e.
just have the first line of the main function) the code compiles
perfectly.
Has anyone got any idea to what Im doing wrongly? - from reading the
cos function takes a double as an argument and returns a double

Thanks in advance,
Nick

Mar 22 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In article <11**********************@y80g2000hsf.googlegroups .com>,
polas <ni**@helpforce.comwrote:
>Hi - I have a problem with a simple piece of code and I can not for
the life of me work out how to fix it. The following code provides an
example:

#import <stdio.h>
#import <math.h>

int main(void)
{
cos((double) 12);
double result=cos((double) 12);
return 0;
}
Um, er, what language is *that*?

Mar 22 '07 #2

P: n/a
In article <11**********************@y80g2000hsf.googlegroups .com>,
polas <ni**@helpforce.comwrote:
>Whenever I compile this, it results in the error message In function
`main':test.c:(.text+0x1b): undefined reference to `cos'
You probably have to include the maths library, perhaps with "-lm"
on the command line.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 22 '07 #3

P: n/a
On 22 Mar, 12:50, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <1174567599.002784.290...@y80g2000hsf.googlegroups .com>,

polas <n...@helpforce.comwrote:
Whenever I compile this, it results in the error message In function
`main':test.c:(.text+0x1b): undefined reference to `cos'

You probably have to include the maths library, perhaps with "-lm"
on the command line.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Thats great thanks very much - done the trick and it works now :)

Mar 22 '07 #4

P: n/a
polas <ni**@helpforce.comwrote:
Hi - I have a problem with a simple piece of code and I can not for
the life of me work out how to fix it. The following code provides an
example:
#import <stdio.h>
#import <math.h>
Shouldn't that be

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

Better always post the exact code you where using, not something
you type in from memory.
int main(void)
{
cos((double) 12);
double result=cos((double) 12);
Do you have a C99 compiler or did you try to compile it with a
C++ compiler? Otherwise all variable definitions must come be-
fore the first statement.
return 0;
}
Whenever I compile this, it results in the error message In function
`main':test.c:(.text+0x1b): undefined reference to `cos'
I guess it's a FAQ: you didn't link against the math library (note:
just including <math.hisn't enough, that's only for the compiler
to tell it what the math functions return and what arguments they
take, you may need something like '-lm' add the end of the command
you use to compile and link the program or something similar depen-
ding on which compiler you are using to link against the math li-
brary).
However, when I do not store or have any reference to the result (i.e.
just have the first line of the main function) the code compiles
perfectly.
If the result of the call od cos() never gets used the compiler is
probably clever enough to detect this and it thus simply throws out
the call completely, so that the cos() function isn't used anymore
at all.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Mar 22 '07 #5

P: n/a
On 22 Mar, 12:59, j...@toerring.de (Jens Thoms Toerring) wrote:
polas <n...@helpforce.comwrote:
Hi - I have a problem with a simple piece of code and I can not for
the life of me work out how to fix it. The following code provides an
example:
#import <stdio.h>
#import <math.h>

Shouldn't that be

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

Better always post the exact code you where using, not something
you type in from memory.
int main(void)
{
cos((double) 12);
double result=cos((double) 12);

Do you have a C99 compiler or did you try to compile it with a
C++ compiler? Otherwise all variable definitions must come be-
fore the first statement.
return 0;
}
Whenever I compile this, it results in the error message In function
`main':test.c:(.text+0x1b): undefined reference to `cos'

I guess it's a FAQ: you didn't link against the math library (note:
just including <math.hisn't enough, that's only for the compiler
to tell it what the math functions return and what arguments they
take, you may need something like '-lm' add the end of the command
you use to compile and link the program or something similar depen-
ding on which compiler you are using to link against the math li-
brary).
However, when I do not store or have any reference to the result (i.e.
just have the first line of the main function) the code compiles
perfectly.

If the result of the call od cos() never gets used the compiler is
probably clever enough to detect this and it thus simply throws out
the call completely, so that the cos() function isn't used anymore
at all.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
Right, yes your quite right Jens it should have been #include although
the compiler did not throw an error with #import. Yes, I am using a
C99 compatible compiler. I had a look in the FAQ, but I think what was
throwing me off was the fact that when not using the return value it
compiled (I thought what you say might be the case, so I tried an
arbitrary function name which I knew didnt exist and even if I did not
use the return result it quite rightly gave an error, which did not
help in this context only confused me further!)

Cheers,
Nick

Mar 22 '07 #6

P: n/a
In article <11**********************@y66g2000hsf.googlegroups .com>,
polas <ni**@helpforce.comwrote:
(JTT wrote)
>If the result of the call od cos() never gets used the compiler is
probably clever enough to detect this and it thus simply throws out
the call completely, so that the cos() function isn't used anymore
at all.
....
>Right, yes your quite right Jens it should have been #include although
the compiler did not throw an error with #import. Yes, I am using a
C99 compatible compiler. I had a look in the FAQ, but I think what was
throwing me off was the fact that when not using the return value it
compiled (I thought what you say might be the case, so I tried an
arbitrary function name which I knew didnt exist and even if I did not
use the return result it quite rightly gave an error, which did not
help in this context only confused me further!)
Note that for this to work, the compiler has to "know" that cos()
doesn't have any side effects. I.e., you can't do this with just any
function.

It seems like it would be possible to break this somehow - that is,
provide a "cos" function that did something other than the default one
in the math library - and which had side effects.

Though I suppose doing so would be OT here...

Mar 22 '07 #7

P: n/a
On Mar 22, 5:46 am, "polas" <n...@helpforce.comwrote:
Hi - I have a problem with a simple piece of code and I can not for
the life of me work out how to fix it. The following code provides an
example:

#import <stdio.h>
#import <math.h>

int main(void)
{
cos((double) 12);
double result=cos((double) 12);
return 0;

}

Whenever I compile this, it results in the error message In function
`main':test.c:(.text+0x1b): undefined reference to `cos'

However, when I do not store or have any reference to the result (i.e.
just have the first line of the main function) the code compiles
perfectly.
Has anyone got any idea to what Im doing wrongly? - from reading the
cos function takes a double as an argument and returns a double

Thanks in advance,
Nick
BTW, it's a FAQ:

14.3: I'm trying to do some simple trig, and I am #including <math.h>,
but I keep getting "undefined: sin" compilation errors.

A: Make sure you're actually linking with the math library. For
instance, under Unix, you usually need to use the -lm option, at
the *end* of the command line, when compiling/linking. See also
questions 13.25, 13.26, and 14.2.

Mar 22 '07 #8

P: n/a
Kenny McCormack wrote, On 22/03/07 14:03:
In article <11**********************@y66g2000hsf.googlegroups .com>,
polas <ni**@helpforce.comwrote:
(JTT wrote)
>>If the result of the call od cos() never gets used the compiler is
probably clever enough to detect this and it thus simply throws out
the call completely, so that the cos() function isn't used anymore
at all.
...
>Right, yes your quite right Jens it should have been #include although
the compiler did not throw an error with #import. Yes, I am using a
C99 compatible compiler. I had a look in the FAQ, but I think what was
throwing me off was the fact that when not using the return value it
compiled (I thought what you say might be the case, so I tried an
arbitrary function name which I knew didnt exist and even if I did not
use the return result it quite rightly gave an error, which did not
help in this context only confused me further!)

Note that for this to work, the compiler has to "know" that cos()
doesn't have any side effects. I.e., you can't do this with just any
function.

It seems like it would be possible to break this somehow - that is,
provide a "cos" function that did something other than the default one
in the math library - and which had side effects.

Though I suppose doing so would be OT here...
Discussion of whether it is "legal" and whether or not the compiler can
do something you might consider "wrong" is topical.

If you wrote your own static function called cos and did not include
math.h then the compiler would be obliged to use your function and not
what it knows about the standard cos function.

If you wrote a non-static function called cos then that invoked
undefined behaviour and so anything is allowed to happen. This gives the
compiler license to *assume* that it will be the standard cos function
and thus inline it within the code ignoring your definition and, if you
do not use the result, not bother even doing that. So one likely result
of the undefined behaviour, particularly with the OPs platform, is that
if he calls cos without using the result then his "replacement" cos
would not actually be called either.

So in Kenny's terms it probably would "break", but that is OK because
the standard allows it to.

Yes, I know the "Kenny is a troll" bit, but I thought the OP might be
interested in the answer to this.
--
Flash Gordon
Mar 22 '07 #9

P: n/a
Kenny McCormack <ga*****@xmission.xmission.comwrote:
>polas <ni**@helpforce.comwrote:
>>#import <stdio.h>
#import <math.h>

Um, er, what language is *that*?
When you're writing stuff in four different languages on a regular
basis, this kind of stuff happens all the time. :)

-Beej

Mar 22 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.