Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)


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


share|improve this question
what happens when you compile this code. – green 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

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

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

Your Answer


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.