Jump to content

Erro função sin com implementar corretamente -lm


Recommended Posts

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:


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

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

collect2: ld returned 1 exit status


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.


Link to comment
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.