PDA

View Full Version : [SOLVED] undefined reference to 'pow'



jon zendatta
April 29th, 2008, 04:13 AM
hell0 people

doing some simple C..but some problems with the math functions even though i am using the math header -- #include <stdio.h>
#include <math.h>
problem being it will not compile! :KS
ps:sorry i probably should be posting this in the absolute beginner's forum

LaRoza
April 29th, 2008, 04:16 AM
hell0 people

doing some simple C..but some problems with the math functions even though i am using the math header -- #include <stdio.h>
#include <math.h>
problem being it will not compile! :KS
ps:sorry i probably should be posting this in the absolute beginner's forum

Add this:



gcc -lm


And whatever else you use, just make sure to have -lm.

jon zendatta
April 29th, 2008, 04:23 AM
cheers
so is this how i compile?

gcc -lm file file.c

dwhitney67
April 29th, 2008, 05:02 AM
You should read the man-pages!

$ man gcc
$ man pow

To compile:

$ gcc -o myFile myFile.c -lm

Don't name your executable 'file', because a binary with that name already exists in /usr/bin.

(The -lm does not have to be at the end; that's just my preference.)

jon zendatta
April 29th, 2008, 05:17 AM
thank you lar0za/dwhitney67
that works good, linux rules:KS

roho@Lenovo
November 15th, 2011, 04:09 AM
-lm being at the end or at the start makes a diference to the linker. Don't know why

gavin_attoe
November 15th, 2011, 05:45 AM
(The -lm does not have to be at the end; that's just my preference.)

Well it looks like this is pretty well all solved. I just thought I'd add my preferred placement of -lm as the innards of a sandwich of file names.


$ cc myFile.c -lm -o myFile.out

Yummy.

schauerlich
November 15th, 2011, 04:38 PM
-lm being at the end or at the start makes a diference to the linker. Don't know why

I don't believe it should. Most likely, placing it at the beginning or end is inadvertently interfering with another flag.

disregard that, i'm wrong.

MadCow108
November 15th, 2011, 08:27 PM
I don't believe it should. Most likely, placing it at the beginning or end is inadvertently interfering with another flag.

it does.
this is always the case with static libraries and also for shared libraries when linking with --as-needed (now default in ubuntu).
gcc handles its commandline from left to right and references must be registered before they are provided.

schauerlich
November 15th, 2011, 08:41 PM
it does.
this is always the case with static libraries and also for shared libraries when linking with --as-needed (now default in ubuntu).
gcc handles its commandline from left to right and references must be registered before they are provided.

Well, shows how much I know. :)