Ask Your Question
0

An easy question about gcc.

asked 2013-08-23 22:33:34 -0600

ptzhan gravatar image

updated 2013-08-23 22:45:32 -0600

There are two codes with very tiny differences. The first code works with the command "gcc xxxx.c"

#include <stdio.h>
#include <math.h>
main()
{int a;
a=sqrt(9);
printf("%d",a);
}

While the other code must work with the command "gcc xxxx.c -lm"

#include <stdio.h>
#include <math.h>
main()
{int a,b=9;
a=sqrt(b);
printf("%d",a);}

Why does the second code need "-lm"? Or why does not the first code need "-lm"?

edit retag flag offensive close merge delete

Comments

one thing you should have to remember is please write a prototype to main section while writing a program

gireesh333 gravatar imagegireesh333 ( 2013-08-23 23:31:13 -0600 )edit

This question fit more into Stackoverflow than here. Also, as a side note, according to your question title, if it is an easy question, why do you ask? ;)

Jomoos gravatar imageJomoos ( 2013-08-25 01:59:31 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-08-24 03:08:54 -0600

dfr gravatar image

In first example sqrt(9) is optimized away by compiler, so in linking phase there is no sqrt call, because compiler replaced it with value.

In second case compiler not optimizing it, so you should link math library explicitly.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-08-23 22:33:34 -0600

Seen: 95 times

Last updated: Aug 24 '13