I am programming a c project which must use the pow function defined in math.h.
And when I tried to make the project, gcc gave the following link error:
undefined reference to `pow’.
I know the -lm option must be added into my link instruction, but there are still several questions puzzling me.
Q1:When I pass two constants into pow function, link is successful without -lm. Why?
Q2:-lm being at the end or at the start of link instruction makes different results. gcc -lm $(OBJS) -o exbin is wrong, but gcc $(OBJS) -o exbin -lm is correct. Why?
I use ubuntu 11.10 and gcc 4.4.4.
Thanks! Please excuse my pool English.
I cannot answer question 1 (that seems odd), but in regards to question 2 the reason
gcc -lm $(OBJS) -o exbindoes not work is because you must link things in order of useage. This is best explained by example:To properly link
func_aandfunc_binto an executable, you must link them asgcc func_b func_a -o execbecausefunc_busesfunc_a. In short, you always want to link your library functions last.