Jump to content
jonhhy

Erro função sin com implementar corretamente -lm

Recommended Posts

jonhhy

Boa tarde,

Fiz um programa em que utilizo a função sin().

A esta função inclui a biblioteca math.h pois dela faz parte.

No terminal tenho de implementar um -lm no com gcc.

Como posso fazê-lo? gcc -lm??:

anexo do erro e causas:

erro:

/tmp/ccNbXz8l.o: In function `main':

tip4#a.c:(.text+0x23): undefined reference to `sin'

collect2: ld returned 1 exit status

causas:

down vote accepted

You have compiled your code with references to the correct math.h header file, but when you attempted to link it, you forgot the option to include the math library. As a result, you can compile your .o object files, but not build your executable.

As Paul has already mentioned add "-lm" to include the math library in the step where you are attempting to generate your executable.


why for sin (math.h), we need -lm option explicitly but not for printf() fn defined in stdio.h, I doubt on GNU's linker capabilities. As in VCC it works and on mac too as flarn2006 mentioned.

Because the math libraries are located in a non-standard to some, yet so commonly accepted it might as well be standard location, and is standard to some. History has conspired to keep it there, as changing it now would break more than it fixes. That's why it's not #include <stdmath.h>, but #include <math.h>. Back then, math implementations might actually be changed for better ones (faster, less memory, more accurate, but likely not all three). So it's really a standardly found not "std" standard library, implemented by libm-version.so in your /usr/lib or /usr/lib64 directory.

from:http://stackoverflow.com/questions/5005363/undefined-reference-to-sin-in-c

Share this post


Link to post
Share on other sites
HappyHippyHippo

sim, necessitas de fazer a linkagem à biblioteca m através desse parâmetro adicional do gcc

qual o comando (completo) do gcc que usas para compilar o teu código ?

Edited by HappyHippyHippo

IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites
jonhhy

Boa tarde, o comando é:

gcc -Wall program.c -o program -lm já resulta e faz inclusive o executável. há que use o -c...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.