• C

what does "cc file.c -lm" (the -lm) part do?


i have been trying to compile a file that looks like this:

#include <math.h>

int main(void)
   double un2 = ceil(3.23923);
  return 0;

all day long.  i was compiling using cc file.c, but i kept getting an error that said "
undefined first referenced symbol in file ceil file.c.  ld: fatal: symbol referencing errors. no output written to a.out

but once i tried doing: cc file.c -lm, it works fine.  why?  What is the secret?  And, is there something important that i am missing? (i am a newbie to c)

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi alexthecodepoet,

-lm links to maths library


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alexthecodepoetAuthor Commented:

thanks for the prompt reply! i really appreciate it.

would you mind expanding a little bit more please?  I know what linking and libraries are, but what are some things that i should be aware of?

Will i have to do -la -lm -lX -l?? for every include file?  Or, is -lm a blanket for all of them?

Is there a better way to go about solving this?

-l flag means link to
m denotes the maths library ...

Linking to library has nothing to do with include file .. both are very different things ..

You are by default linked to glibc .. Hence you do not need explicit linking flags for printf etc. Hpwever, definition for maths functions such as ciel floor etc are part of separate maths library which needs to be linked explicitly ...

You cannot link to random alphabets .. libraries have fixed names ... e.g. if you were to use pthreads library, you will have to use the flag -lpthreads

These libraries are configured through the /etc/ld.so.conf file
alexthecodepoetAuthor Commented:
oh, i see, well thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.