LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-08-2010, 02:35 PM   #1
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
What exactly does the -lm option of gcc?


Hello together,

I'm learning C and found this problem. the following code:
Code:
/* sqroot.c */

#include <stdio.h>
#include <math.h>

int main()
{
	double x, y;
	x = 15.0;
	printf("The square root of %lf is %lf\n", x, sqrt(x));
}
does not compile, the error-message is:
Code:
/tmp/ccio1OHj.o: In function `main':
sqroot.c:(.text+0x34): undefined reference to `sqrt'
collect2: ld gab 1 als Ende-Status zurück
with the following command:
Code:
gcc -o sqroot sqroot.c
With the search funktion of this forum I found this thread: http://www.linuxquestions.org/questi...rt#post3737549 which suggests to use the -lm option with gcc, this works for me,
Code:
gcc -lm -o sqroot sqroot.c
but what does this option mean for gcc? In the manpage of gcc there is an explanation of an -l option for the linker but I don't think that it is the same as -lm.

Another question is: why does "sqrt(15.0)" instead of "sqrt(x)":
Code:
printf("The square root of %lf is %lf\n", x, sqrt(15.0));
in the above programm work without the -lm option?

Markus
 
Old 02-08-2010, 03:04 PM   #2
rlhartmann
Member
 
Registered: Mar 2008
Posts: 73

Rep: Reputation: 16
The -l means link a library. The m means the math library. You need to link the math library. This is the standard library that has common math functions like sqrt, sin, cos, log, etc. Linking makes these functions available to the main progra.m

For your second question, x is defined as a double precision variable, since x
has been assigned to 15.0, then sqrt(15.0) and the sqrt(x) will give the same result.
Usually you want to use sqrt(x) because the value of x may change within to program.
for example, you may want to write a program that asks the user to input a number, then
calculate the square root of the number they entered.
 
Old 02-08-2010, 03:06 PM   #3
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by markush View Post
In the manpage of gcc there is an explanation of an -l option for the linker but I don't think that it is the same as -lm.
It should be the same. -l is the option, m is value used by the option.

It means gcc should pass instructions to the linker to look for libm.so or libm.a to be searched to resolve undefined references.

Quote:
gcc -lm -o sqroot sqroot.c
I think that is wrong. I think -lm has to come on the command line after the .c file that needs it
Quote:
Another question is: why does "sqrt(15.0)" instead of "sqrt(x)":
Code:
printf("The square root of %lf is %lf\n", x, sqrt(15.0));
in the above programm work without the -lm option?
I think that may be a result of the optimization level. It may be optimizing enough to know sqrt(15.0) is constant and computing it at compile time, but not enough to notice x is 15.0 and thus also known at compile time.

Last edited by johnsfine; 02-08-2010 at 03:13 PM.
 
1 members found this post helpful.
Old 02-08-2010, 11:36 PM   #4
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Quote:
Originally Posted by johnsfine View Post
I think that is wrong. I think -lm has to come on the command line after the .c file that needs it
It isn't (why would it be?).
 
Old 02-09-2010, 12:28 AM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Original Poster
Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
Hello together,

thank you for your explanations. I think that I've now understood this issue.

Markus
 
Old 02-10-2010, 08:58 AM   #6
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by carbonfiber View Post
It isn't (why would it be?).
Older C compilers were sensitive to library ordering. They could look ahead, but not behind. Newer versions of GNU compilers don't necessarily have this issue, but older versions, or non-GNU compilers may have this issue.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc/g++ option nuxrl Programming 1 11-30-2006 01:36 PM
about -00 option of gcc George2 Programming 3 08-29-2006 03:28 PM
gcc -I option vkmgeek Programming 18 05-16-2006 06:27 PM
what does the option -s of gcc mean ? hongxing Linux - Software 3 11-28-2005 08:12 PM
gcc option -E use. niverson Programming 2 10-11-2004 04:06 PM


All times are GMT -5. The time now is 04:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration