Ask Your Question

An easy question about gcc.

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

ptzhan gravatar image

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

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

#include <stdio.h>
#include <math.h>
{int a;

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

#include <stdio.h>
#include <math.h>
{int a,b=9;

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

edit retag flag offensive close merge delete


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 -0500 )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 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

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

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


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

Seen: 99 times

Last updated: Aug 24 '13