C Error Undefined Reference To Sqrt
Contents |
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 c programming undefined reference to sqrt ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join gcc undefined reference to sqrt 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 undefined reference to sqrt eclipse minute: Sign up Why am I getting “undefined reference to sqrt” error even though I include math.h header? up vote 34 down vote favorite 12 I'm very new to C and I have this code: #include
Undefined Reference To Pow
{ double x = 0.5; double result = sqrt(x); printf("The square root of %lf is %lf\n", x, result); return 0; } But when I compile this with: gcc test.c -o test I get an error like this: /tmp/cc58XvyX.o: In function `main': test.c:(.text+0x2f): undefined reference to `sqrt' collect2: ld returned 1 exit status Why does this happen? Is sqrt() not in the math.h header file? I get the same error with cosh and other trigonometric functions. Why? c linker libm share|improve this question undefined reference to sqrt ubuntu edited Dec 13 '14 at 17:11 Jonathan Leffler 438k61508821 asked May 2 '12 at 6:53 Ant's 4,951863116 And here is some speculation why about why libm isn't linked by default even though it contains part of the standard library: stackoverflow.com/questions/1033898/… –Michael Burr May 2 '12 at 7:01 It's a duplicate to this one: stackoverflow.com/q/5248919/694576 –alk May 2 '12 at 7:10 add a comment| 4 Answers 4 active oldest votes up vote 54 down vote accepted The math library must be linked in when building the executable. How to do this varies by environment, but in Linux/Unix, just add -lm to the command: gcc test.c -o test -lm The math library is named libm.so, and the -l command option assumes a lib prefix and .a or .so suffix. share|improve this answer edited May 2 '12 at 8:12 answered May 2 '12 at 6:55 wallyk 41.9k84898 add a comment| up vote 5 down vote You need to link the with the -lm linker option You need to compile as gcc test.c -o test -lm gcc (Not g++) historically would not by default include the mathematical functions while linking. It has also been separated from libc onto a separate library libm. To link with these functions you have to advise the linker to include the library -l linker option followed by the library name m thus -lm. share|improve this answer edited May 2 '12 at 7:00 answered May 2 '12 at 6:55 Abhijit 36k64911
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 undefined reference to `sqrt' makefile about Stack Overflow the company Business Learn more about hiring developers or posting
How To Include Math.h In Gcc
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Undefined Reference To `__errno'
Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up sqrt() function link error up vote 2 down vote favorite The http://stackoverflow.com/questions/10409032/why-am-i-getting-undefined-reference-to-sqrt-error-even-though-i-include-math following code is throwing undefined symbol error on Linux. $ cat rms.c /* sqrt example */ #include
Programming Boards C Programming undefined reference to `sqrt' when using math.h Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: undefined http://cboard.cprogramming.com/c-programming/101188-undefined-reference-%60sqrt-when-using-math-h.html reference to `sqrt' when using math.h Thread Tools Show Printable Version Email this Page… Subscribe https://bytes.com/topic/c/answers/519717-how-use-sqrt-function-gcc to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 03-31-2008 #1 Milhas View Profile View Forum Posts Banned Join Date Mar 2008 Posts 78 undefined reference to `sqrt' when using math.h Just like the title says.. I get undefined reference to `sqrt' error and i included math.h Using GCC by the way, in linux. 03-31-2008 #2 Cactus_Hugger View Profile undefined reference View Forum Posts int x = *((int *) NULL); Join Date Jul 2003 Location Banks of the River Styx Posts 902 "undefined reference" is a linker error - meaning that you didn't link with the code for the function it's complaining about. (Most of the times, #including something will not fix this) For sqrt(), add -lm to your linker flags. long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable undefined reference to from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) 03-31-2008 #3 Milhas View Profile View Forum Posts Banned Join Date Mar 2008 Posts 78 Originally Posted by Cactus_Hugger "undefined reference" is a linker error - meaning that you didn't link with the code for the function it's complaining about. (Most of the times, #including something will not fix this) For sqrt(), add -lm to your linker flags. What is a linker flags? I'm using gcc how should i do it? 03-31-2008 #4 Cactus_Hugger View Profile View Forum Posts int x = *((int *) NULL); Join Date Jul 2003 Location Banks of the River Styx Posts 902 Example: Code: $ gcc -c -o main.o main.c $ gcc -c -o second.o second.c $ gcc -o myprogram main.o second.o -lm or, if you're doing it all in one line: $ gcc -o myprogram main.c second.c -lm long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) 03-31-2008 #5 Milhas View Profile View Forum Posts Banned Join D
question and get tips & solutions from a community of 418,461 IT Pros & Developers. It's quick & easy. How to use sqrt() function with gcc? P: n/a John I'm trying to use the sqrt() function in Linux with GCC v3.3.6, compiling with this command: $ gcc sourcefile.c I get this error: In function 'main': undefined reference to 'sqrt' collect2: ld returned 1 exit status Here is my source code: #include