LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-14-2001, 12:30 AM   #1
michael_hoare
LQ Newbie
 
Registered: Oct 2001
Distribution: RH7.1
Posts: 5

Rep: Reputation: 0
gcc compile problems


I am a newbie to LINUX and C generally. I am using RH71, and trying to learn C.

















I am trying to compile a simple program in C, using the gcc compiler. Every time I try to use functions sqrt or pow, I get a compile error

















undefined reference to "sqrt"








( or undefined refernce to "pow", if I use that function)

















I tried compiling under Windows, and it works fine, so the code is probably OK

















I tried compiling under the KDevelop IDE, and got the same error.

















I tried commenting out the line #include <math.h>, and got the same error, which may mean that the compiler can't find the header files (but it finds stdio.h alright).

















I tried directing the incude direction to /usr/include/math.h, but I get the same error.

















I tried looking through the gcc documentation, which is comprehensive but hard work, and couldn't find any answers.

















Do I need to set up or configure gcc in some way? What else should I check? Any help would be very much appreciated.
 
Old 10-14-2001, 02:04 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
I just wrote a small test program that used pow and I got the following linker error when compiling:

Code:
$ cc mathtest.c 
/tmp/ccFv4vgE.o: In function `main':
/tmp/ccFv4vgE.o(.text+0x12): undefined reference to `pow'
collect2: ld returned 1 exit status
This is a linker error stating that it can't find the library for pow.

If you go to /usr/lib and type the command: nm libm.a | grep pow

you will see that pow is in that libraty. You can get it to compile correctly by instructing the linker to use the m (math) library like this:

Code:
cc mathtest.c -lm
My test program worked well:

mathtest
pow( 5, 3 ) = [125.000000]


Gary
 
Old 10-15-2001, 12:30 AM   #3
toxic53
Member
 
Registered: Sep 2001
Posts: 30

Rep: Reputation: 15
This is a common mistake when using pow and anything the math.h when compiling w/ linux:


whenever you use the math.h library do this:


gcc -o outputfile inputfile.c -lm

I dont know why you have to have the -lm its stupid but necessary thats not a one its and L in lowercase...this will work

~Jesus~
 
Old 10-15-2001, 02:00 AM   #4
michael_hoare
LQ Newbie
 
Registered: Oct 2001
Distribution: RH7.1
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks

Thanks for the help - I worried at this for a few hours. before posting, which is part of the learning curve. Just my luck that the first header I try to use other than stdio.h happened to be math.h with an interesting error.
 
Old 12-19-2001, 09:19 AM   #5
linuxtard
LQ Newbie
 
Registered: Dec 2001
Distribution: RedHat 7.2
Posts: 2

Rep: Reputation: 0
Compiling the math.h library in KDevelop

For anyone interested in getting KDevelop to compile with the math.h library, in addition to the "#include <math.h>" in your program, I found you have to edit the "Makefile" file in your "projectname/projectname" directory and find the line that says

all_libraries =

and make it look like

all_libraries = -lm (that's -LM if it were uppercase)
 
Old 01-01-2002, 06:18 AM   #6
cheezwhiz
LQ Newbie
 
Registered: Dec 2001
Posts: 4

Rep: Reputation: 0
Hi, I asked the same question some days ago on another board, here is the link for all interested:

http://www.cprogramming.com/cboard/s...?threadid=7808

It is explained why you have to use the -lm option
 
Old 02-05-2002, 08:38 AM   #7
linuxtard
LQ Newbie
 
Registered: Dec 2001
Distribution: RedHat 7.2
Posts: 2

Rep: Reputation: 0
Compiling with the Math Library in KDevelop

I just discovered what's probably easier than editing the Makefile everytime you add a file to your project....simply go to the Project->Options->Linker Options menu and check the box that says "math". I haven't tried it yet but I'm sure this is what I should have done in the first place.
 
  


Reply



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
Compile GCC 3.3 mullins Linux - Software 1 08-04-2005 06:44 PM
compile C++ with GCC phongnh0211k Linux - Software 3 01-13-2005 01:40 AM
gcc can't compile zionz Linux - Newbie 2 09-02-2004 11:34 PM
gcc 3.4.1 want not to compile, but 3.2.2 does BorisB Linux - Software 3 07-27-2004 06:53 PM
How to compile GCC if you don't have GCC? khermans Linux - Software 13 05-12-2004 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:19 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration