This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Using Math Functions


[copied back to gcc list, so that nobody else sends this FGA]

>>>>> "calvesmit" == calvesmit  <calvesmit at ig dot com dot 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 at ig dot com dot 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 at gcc dot gnu dot 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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]