* Re: Using Math Functions
[not found] <200303040600.h2460KB0000000990@e-eihq01.eis.ernet.in>
@ 2003-03-04 8:47 ` Anjali Sharma
0 siblings, 0 replies; 4+ messages in thread
From: Anjali Sharma @ 2003-03-04 8:47 UTC (permalink / raw)
To: calvesmit, gcc
Hi
Trying using "-lm" while linking your program
Hope this helps
Anjali
uucp-relay-delhi!calvesmit@ig.com.br wrote:
> I'm trying to compile the code below, but I can't to do it! gcc always
> complain.
> could someone help me? Why my sistem seems not know about math functions
> althoug i use #include math.h inclusion.
> send any hint to calvesmit@ig.com.br, 'cause I'm not a subscribe of your
> list.
>
> **********CODE***********
> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
>
> int main(int argc,char *argv[])
> {
> int i;
> if(argc==2){
> i=atoi(argv[1]);
> printf("sin(%i)=%0.2f\n",i,sin(i));}
> return 0;
> }
>
> **************ERROR MESSAGE*************
> $gcc sin.c -osin
> /root/tmp/ccpbz3XU.o: In function 'main':
> /root/tmp/ccpbz3XU.o(.text+0x32): undefined reference to 'sqrt'
> collect2: ld returned 1 exit status
>
> _________________________________________________________
> Voce quer um iGMail protegido contra vírus e spams?
> Clique aqui: http://www.igmailseguro.ig.com.br
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Using Math Functions
2003-03-04 7:32 ` Tkil
@ 2003-03-04 7:37 ` Tkil
0 siblings, 0 replies; 4+ messages in thread
From: Tkil @ 2003-03-04 7:37 UTC (permalink / raw)
To: calvesmit; +Cc: gcc
>>>>> "Tkil" == tkil <tkil@scrye.com> writes:
Tkil> Or, if you have multiple files:
Tkil> gcc -c my_lib.c
Tkil> gcc -c my_prog.c
Tkil> gcc -o my_prog my_lib.o my_prog.o -lm
^^^^^^^^ ^^^^^^^^^^
Open foot, insert mouth. Assuming my_prog.c calls routines in
my_lib.c, it needs to be the other way around:
gcc -o my_prog my_prog.o my_lib.o -lm
Tkil> Note that "-lm" should be after any of your object files that
Tkil> use functions from it, as symbols are resolved from
Tkil> left-to-right on the command line.
I even knew that.
t.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Using Math Functions
[not found] <gfzq31xed.fsf@totally-fudged-out-message-id>
@ 2003-03-04 7:32 ` Tkil
2003-03-04 7:37 ` Tkil
0 siblings, 1 reply; 4+ messages in thread
From: Tkil @ 2003-03-04 7:32 UTC (permalink / raw)
To: calvesmit; +Cc: gcc
[copied back to gcc list, so that nobody else sends this FGA]
>>>>> "calvesmit" == calvesmit <calvesmit@ig.com.br> writes:
calvesmit> I'm trying to compile the code below, but I can't to do it!
calvesmit> gcc always complain. could someone help me? Why my sistem
calvesmit> seems not know about math functions althoug i use #include
calvesmit> math.h inclusion. send any hint to calvesmit@ig.com.br,
calvesmit> 'cause I'm not a subscribe of your list.
Short answer: you need "-lm" at the end of your gcc line.
Longer answer:
Using "#include <math.h>" only brings in the *declarations* of the
functions (type of arguments, type of return); it doesn't bring in the
*definition* or *implementation* of the functions. Those are stored
in the math library, "libm.a" or "libm.so". You link these in by
using "-lm" on your compile line:
gcc -o my_prog my_prog.c -lm
Or, if you have multiple files:
gcc -c my_lib.c
gcc -c my_prog.c
gcc -o my_prog my_lib.o my_prog.o -lm
Note that "-lm" should be after any of your object files that use
functions from it, as symbols are resolved from left-to-right on the
command line.
Next time, consider searching the web before posting; a quick search
for the text of your error gave me the answer on the second hit.
(Granted, I already *knew* the answer, but hey...)
Finally, note that this list (<gcc@gcc.gnu.org>) is explicitly for the
development of GCC itself -- it is not for developing *with* gcc; your
question would be better answered on comp.lang.c or similar fora.
Oh, and the really last thing: bookmark Google, and use it often (I do
understand that you might not be on the 'net full time, but you were
on long enough to send e-mail); also, consult the C FAQ, which is a
very helpful document:
http://www.eskimo.com/~scs/C-faq/top.html
Regarding your question in particular:
http://www.eskimo.com/~scs/C-faq/q14.3.html
If you get the chance, please read the entire document. You might not
understand all of it the first time through, but it will hopefully
live in your memory; the next time you have a question, you might
remember where to find the answer, even if you don't remember it
offhand. :)
Happy hacking,
t.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Using Math Functions
@ 2003-03-04 5:35 calvesmit
0 siblings, 0 replies; 4+ messages in thread
From: calvesmit @ 2003-03-04 5:35 UTC (permalink / raw)
To: gcc
I'm trying to compile the code below, but I can't to do it! gcc always
complain.
could someone help me? Why my sistem seems not know about math functions
althoug i use #include math.h inclusion.
send any hint to calvesmit@ig.com.br, 'cause I'm not a subscribe of your
list.
**********CODE***********
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc,char *argv[])
{
int i;
if(argc==2){
i=atoi(argv[1]);
printf("sin(%i)=%0.2f\n",i,sin(i));}
return 0;
}
**************ERROR MESSAGE*************
$gcc sin.c -osin
/root/tmp/ccpbz3XU.o: In function 'main':
/root/tmp/ccpbz3XU.o(.text+0x32): undefined reference to 'sqrt'
collect2: ld returned 1 exit status
_________________________________________________________
Voce quer um iGMail protegido contra vÃrus e spams?
Clique aqui: http://www.igmailseguro.ig.com.br
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-03-04 7:54 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <200303040600.h2460KB0000000990@e-eihq01.eis.ernet.in>
2003-03-04 8:47 ` Using Math Functions Anjali Sharma
[not found] <gfzq31xed.fsf@totally-fudged-out-message-id>
2003-03-04 7:32 ` Tkil
2003-03-04 7:37 ` Tkil
2003-03-04 5:35 calvesmit
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).