Error Undefined Symbol .sqrt
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 C - undefined reference to “sqrt” even with '-lm' up vote 8 down vote favorite I try to compile a library in C that need "math.h", here is the begining of the .c file: #include #include #include #include #include "sparse_matrix.h" ... and I compile with this command: gcc -c ./sparse_matrix.c -o sparse_matrix.o -lm -Wall -pedantic -std=c99 -g -O But even with the #include done and the flag -lm after the file (I've tried at the end of the line but nothing changed) I still get the error: undefined reference to « sqrt » collect2: error: ld returned 1 exit status I don't get it after an hour of googling the issue. I'm working with gcc 4.9 under ubuntu 14.10 (utopic unicorn). Thank for any help in advance! c gcc ld math.h share|improve this question asked Dec 15 '14 at 0:57 Alexandre Pieroux 5216 The -c option suppresses linking. You won't get that error from that command line. Therefore, that isn't the command line that produces the error. (Don't use @ before rules in a makefile — it probably isn't your problem, but it hides commands and can mislead you.) And most of this is what @paxdiablo said. –Jonathan Leffler Dec 15 '14 at 1:06 possible duplicate of "undefined reference to `pow'" even with math.h and the library link -lm –Ciro Santilli 烏坎事件2016六四事件 法轮功 May 15 '15 at 20:36 add a comment| 1 Answer 1 active oldest votes up vote 9 down vote accepted I don't think that is the command you're running (well, it may be one of them, but it's certainly not the one causing your error). The -c option to gcc tells it to only create the object files (and you're specifically sending the output to sparse_matrix.o, an object file rather than an executable one). In that case, the linker should not be called at all. In fact, with a dummy sparse_matrix.c of: #include int main(void) { re
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 sqrt() function link error up vote 2 down vote favorite The following http://stackoverflow.com/questions/27475977/c-undefined-reference-to-sqrt-even-with-lm code is throwing undefined symbol error on Linux. $ cat rms.c /* sqrt example */ #include #include int main () { double param, result; param = 1024.0; result = sqrt (param); printf ("sqrt(%lf) = %lf\n", param, result ); return 0; } $ gcc rms.c /tmp/ccaWecFP.o(.text+0x24): In function `main': : undefined reference to `sqrt' collect2: ld returned 1 exit status If I replace argument to sqrt() with (double)16 http://stackoverflow.com/questions/15743330/sqrt-function-link-error then program is compiling and executing. Why is this throwing error in first case. c sqrt share|improve this question edited Apr 1 '13 at 12:41 alk 45k53497 asked Apr 1 '13 at 12:16 Chandu 311416 1 For sqrt() you need to reference the math library --- add -lm to the gcc command line –jim mcnamara Apr 1 '13 at 12:18 possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it? –Colonel Thirty Two Jun 30 '15 at 14:11 I'm voting as "not a duplicate" because the "why does this work with a constant" is not answered over there. –o11c Jul 1 '15 at 1:17 add a comment| 4 Answers 4 active oldest votes up vote 4 down vote This is a linker error. The linker is missing the implementation of sqrt(). It resides in the library libm. Tell GCC to add it by applying the option -lm. share|improve this answer edited Jun 30 '15 at 14:05 answered Apr 1 '13 at 12:20 alk 45k53497 add a comment| up vote 3 down vote The implementation of sqrt() is available in the math library or libm. You have to link your program to the math l
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct Ubuntu Wiki Community Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu Official Documentation User Documentation https://ubuntuforums.org/showthread.php?t=1802431 Social Media Facebook Twitter Useful Links Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: Ubuntu OMG! Ubuntu Ubuntu Insights Planet Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk gcc: undefined reference to `sqrt' even if I included math.h Having an Issue With Posting ? Do you want to help us debug the posting issues ? < is the place to error undefined report it, thanks ! Results 1 to 3 of 3 Thread: gcc: undefined reference to `sqrt' even if I included math.h Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode July 12th, 2011 #1 skytreader View Profile View Forum Posts Private Message A Carafe of Ubuntu Join Date Aug 2009 Beans 124 gcc: undefined reference to `sqrt' even if I included math.h error undefined symbol So I have this C code: Code: #include #include #include double dist(int x1, int y1, int x2, int y2){ double dx = (double) (x1 - x2); double dy = (double) (y1 - y2); double xx = pow(dx, 2.0); double yy = pow(dy, 2.0); double distance = sqrt(xx + yy); return distance; } But gcc complains that: Code: /tmp/cck84m3o.o: In function `dist': astar.c:(.text+0x5d): undefined reference to `sqrt' collect2: ld returned 1 exit status I don't get why gcc would say that. Anything I missed? Adv Reply July 12th, 2011 #2 JupiterV2 View Profile View Forum Posts Private Message Tea Glorious Tea! Join Date Sep 2006 Location BC, Canada Beans 347 DistroUbuntu 10.10 Maverick Meerkat Re: gcc: undefined reference to `sqrt' even if I included math.h You need to link the math library. Use -lmath or -lm (short-hand) flags. Current Project: Vocab Builder Blog: http://noeffclue.blogspot.com Adv Reply July 12th, 2011 #3 dwhitney67 View Profile View Forum Posts Private Message Tolerant of Ubuntu Join Date Jun 2007 Location Maryland, US Beans 6,272 DistroKubuntu Re: gcc: undefined reference to `sqrt' even if I included math.h Originally Posted by skytreader I don't get why gcc would say that. Anything I missed? That's because math.h is merely a header file with function declarations. It does NOT include the implementation of the functions them