Error In Math.h
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why Am I Getting Link Errors When Calling Function in Math.h? up vote 4 down vote favorite 1 When attempting to call functions in math.h, I'm getting link errors like the following undefined reference to sqrt But I'm doing a #include I'm using gcc and compiling as follows: gcc -Wall -D_GNU_SOURCE blah.c -o blah Why can't the linker find the definition for sqrt? math gcc linker share|improve this question asked Sep 19 '08 at 16:21 FreeMemory 5,95342535 add a comment| 2 Answers 2 active oldest votes up vote 6 down vote accepted Add -lm to the command when you call gcc: gcc -Wall -D_GNU_SOURCE blah.c -o blah -lm This will tell the linker to link with the math library. Including math.h will tell the compiler that the math functions like sqrt() exist, but they are defined in a separate library, which the linker needs to pack with your executable. As FreeMemory pointed out the library is called libm.a . On Unix-like systems, the rule for naming libraries is lib[blah].a . Then if you want to link them to your executable you use -l[blah] . share|improve this answer answered Sep 19 '08 at 16:24 Dima 29.2k115495 2 Actually, if you do that on a modern Linux system, you will end up linking to libm.so, which is the dynamic library equivalent of libm.a. –CesarB Nov 18 '08 at 19:44 add a comment| up vote 2 down vote You need to link the math library explicitly. Add -lm to the flags you're passing to gcc so that the linker knows to link libm.a share|improve this answer edited Sep 22 '08 at 17:36 answered Sep 19 '08 at 16:22 FreeMemory 5,95342535 You mean libm.a :) –Dima Sep 19 '08 at 16:25 1 You know, you can fix it, right? :) You can always edit your answers and questions. –Dima Sep 19 '08 at 16:29 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged math gcc linker or ask your own questi
Languages , Windows Desktop Development > C++ Standards, Extensions, and Interop Question 0 Sign in to vote Hi, I have just needed to use the math library for sqrt, cos and log. I have just added the math header into my project and when I compile it I end up with 50 errors detected in the math.h file.Here are the librairies http://stackoverflow.com/questions/103407/why-am-i-getting-link-errors-when-calling-function-in-math-h I include in my main header file: Code Snippet #ifndef _TFG_H_#define _TFG_H_#define _WINSOCKAPI_ // Prevent winsock.h #include's.#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers#define _CRT_SECURE_NO_DEPRECATE#include #include #include #include #include #include #include #include #include #include "sgactl.h"#undef timeval#define timeval WINSOCK2_TIMEVAL#include #undef timeval#define timeval D16_TIMEVAL#include "d16e0182.h"In the .c file where I https://social.msdn.microsoft.com/Forums/vstudio/en-US/20420398-34fb-45b9-a76c-170deb549a33/when-mathh-is-included-detects-errors-in-mathh?forum=vclanguage use the math functions, I add this : Code Snippet #include "tfg.h"#include These are the libraries I include in the project : Code Snippet kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib ws2_32.lib comctl32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /incremental:yes /pdb:"Debug/tfconf.pdb" /debug /machine:I386 /out:"Debug/tfconf.exe" /pdbtype:sept And finally, the best part....the errors ... : Code Snippet :\program files\microsoft visual studio\vc98\include\math.h(74) : error C2054: expected '(' to follow 'd'c:\program files\microsoft visual studio\vc98\include\math.h(124) : error C2085: '_HUGE' : not in formal parameter listc:\program files\microsoft visual studio\vc98\include\math.h(154) : error C2082: redefinition of formal parameter 'abs'c:\program files\microsoft visual studio\vc98\include\math.h(155) : error C2085: 'acos' : not in formal parameter listc:\program files\microsoft visual studio\vc98\include\math.h(156) : error C2085: 'asin' : not in formal parameter listc:\program files\microsoft visual studio\vc98\include\math.h(157) : error C2085: 'atan' : not in formal parameter listc:\program files\microsoft visual studio\vc98\include\math.h(158) : error C2085: 'atan2' : not in formal parameter listc:\program files\microsoft visual studio\vc98\include\math.h(159) :
// http://www.cplusplus.com/reference/cmath/erf/ additional overloads for integral types Compute error function Returns the error function value for x. Header provides a type-generic macro version of this function. Additional overloads are provided in this header () for the integral types: These overloads effectively error in cast x to a double before calculations (defined for T being any integral type). Parameters x Parameter for the error function. Return Value Error function value for x. Example 1
2
3
4
5
6
7
8
9
10
11
12
/* erf example */ #include /* printf */ #include error in math.h /* erf */ int main () { double param, result; param = 1.0; result = erf (param); printf ("erf (%f) = %f\n", param, result ); return 0; } Output: erf (1.000000) = 0.842701 See also erfcCompute complementary error function (function )lgammaCompute log-gamma function (function )tgammaCompute gamma function (function ) C++ Information Tutorials Reference Articles Forum Reference C library: (assert.h) (ctype.h) (errno.h) (fenv.h) (float.h) (inttypes.h) (iso646.h) (limits.h) (locale.h) (math.h) (setjmp.h) (signal.h) (stdarg.h) (stdbool.h) (stddef.h) (stdint.h) (stdio.h) (stdlib.h) (string.h) (tgmath.h) (time.h) (uchar.h) (wchar.h) (wctype.h) Containers: