Undefined reference to math functions when linking with gcc
The libraries being linked to should be specified after there is a reference to them. Thus, you will change the command to:
gcc -g -O2 -fopenmp -L/usr/lib -o lenstool_tab e_nfwg.o lenstool_tab.o midpnt.o nrutil.o polint.o qromo.o read_bin.o lenstool_tab.o -lcfitsio -lm
This should fix your problem. You can possibly fix the problem in your Makefile so that the libraries are specified later.
Related videos on Youtube
c0der
Updated on September 18, 2022Comments
-
c0der almost 2 years
I'm getting an error when using
make
to install a program. The full step that it fails on with error looks like this:gcc -g -O2 -fopenmp -L/usr/lib -lcfitsio -lm -o lenstool_tab e_nfwg.o lenstool_tab.o midpnt.o nrutil.o polint.o qromo.o read_bin.o lenstool_tab.o: In function `main': /usr/local/src/lenstool-6.8/table_src/lenstool_tab.c:73: undefined reference to `log' /usr/local/src/lenstool-6.8/table_src/lenstool_tab.c:73: undefined reference to `log' e_nfwg.o: In function `surfdens2': /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:133: undefined reference to `pow' /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:130: undefined reference to `sin' /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:138: undefined reference to `pow' /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:140: undefined reference to `sin' /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:140: undefined reference to `pow' /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:144: undefined reference to `sin' /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:144: undefined reference to `pow' e_nfwg.o: In function `nfwg_kappa': /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:63: undefined reference to `pow' e_nfwg.o: In function `scmass': /usr/local/src/lenstool-6.8/table_src/e_nfwg.c:165: undefined reference to `pow' collect2: error: ld returned 1 exit status make[1]: *** [lenstool_tab] Error 1 make[1]: Leaving directory `/usr/local/src/lenstool-6.8/table_src' make: *** [all-recursive] Error 1
So obviously there is a problem with linking to math library. I checked that these codes do contain
#include<math.h>
. The main cause of this problem seems to usually be from a lack of-lm
at the compile command, but as you can see it appears in the above. I added the-lm
to various parts of the makefile that seemed appropriate, but it didn't help. I'm including the makefile here in case it helps. Should I be modifying the makefile to fix this? I searched this problem on the web but no answers seemed to consider the makefile. I read a suggestion in another thread to modify config.status to include-lm
, but it was not clear where to do that. I've installed this program on a Mac before without issue but now I'm using Ubuntu 14 and I get this error. Any help is much appreciated! Thanks. -
c0der almost 9 yearsI suspected that might be an issue, thanks for pointing that out. But any ideas about how can I implement this fix? You can see that
-lcfitsio -lm
comes from line 301 in the makefile, how can I set it so that it comes at the end of the gcc commands? Actually I dug a little deeper and found another makefile in the directory with those c programs, and found a similar line to put-lm
on, but it's still not showing up at the end of that command. Or if I could do this step manually without modifying the makefile, I'm not sure how to do it. -
unxnut almost 9 yearsYou will have to check where the final target is being built. I am sure the libraries have been defined using some macros and you just need to move the variables around in the statement.
-
c0der almost 9 yearsWell I spent a lot of time altering the makefiles but I couldn't get it to work. In the end I just entered that gcc command manually and used
make
again to finish it off.