Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I just installed ubuntu 12.04 LTS and am having trouble linking to the math functions (from ). I have gcc installed and able to compile the source fine. However, trying:

gcc -lm montecarlo_time.o parsercfile.o -o trmcfl

Gives several errors (of the form):

montecarlo_time.o: In function main: /home/karthik/Research/MC/trmcfl/Dumps/whiteScaling/18-Sep-2012/montecarlo_time.c:75: undefined reference to asin

However, all of these files are present (and visible to ldconfig -v -N)

/lib/i386-linux-gnu/libm.so.6
/lib64/libm.so.6
/usr/lib/i386-linux-gnu/libm.a
/usr/lib/i386-linux-gnu/libm.so
/usr/lib/i386-linux-gnu/xen/libm.a
/usr/lib64/libm.a
/usr/lib64/libm.so

Why is the linking failing? Any/all help is appreciated.

Thanks!

share|improve this question
    
what happens when you compile this code. –  green7 Sep 18 '12 at 16:28
    
Compiles fine -- no errors. gcc -c -Wall montecarlo_time.c montecarlo_time.c: In function ‘newpath’: montecarlo_time.c:333:28: warning: variable ‘nextlayer_ref_index’ set but not used [-Wunused-but-set-variable] montecarlo_time.c:332:42: warning: variable ‘stepy’ set but not used [-Wunused-but-set-variable] montecarlo_time.c:332:35: warning: variable ‘stepx’ set but not used [-Wunused-but-set-variable] gcc -c -Wall parsercfile.c –  Karthik Sep 18 '12 at 17:32
add comment

2 Answers 2

you should use -lm at the end.

because unresolved symbols are found after gcc encounters montecarlo_time.o and there is no library or anything else in which it can find it can resolve that symbol.

share|improve this answer
    
That worked! Still don't follow why the linker cannot resolve calls to math.h functions, if -lm was the first option to gcc. –  Karthik Sep 18 '12 at 18:31
    
It's a longstanding limitation of the way symbols are resolved. –  hnasarat Sep 20 '12 at 3:43
add comment

I would make this a comment, but don't have the status. I have always typed gcc -lm file.c; Until the most recent version of Ubuntu, this worked. On the internet, I see suggestions that it's bad style, but it works. It seems this has now changed.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.