LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-30-2013, 08:01 AM   #1
islam_assi
LQ Newbie
 
Registered: May 2013
Location: Palestine
Posts: 6

Rep: Reputation: Disabled
error (undefined reference to `pow') while compiling a C code in terminal


Hello
I'm trying to compile a C code in terminal ,but I got this error

/tmp/ccoJICUV.o: In function `main':
exo2_main.c.text+0x76): undefined reference to `pow'
collect2: error: ld returned 1 exit status

although I wrote (#include "math.h") in the main
 
Old 05-30-2013, 08:12 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
It should be in the math library. Add "-lm" to the command used to compile it.
 
1 members found this post helpful.
Old 05-30-2013, 08:34 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by jpollard View Post
It should be in the math library. Add "-lm" to the command used to compile it.
I agree. The rest of what I have to say only explains that answer. It doesn't replace that answer.

Quote:
Originally Posted by islam_assi View Post
although I wrote (#include "math.h") in the main
Both the declaration and the definition of the function are required.

math.h provides the declaration of the function at compile time in a way that requires the definition to be provided at link time.

The -lm provides access to the definition at link time.

We assume a single gcc command was used to both compile and link, so the -lm could be added to that command. If separate commands were used for compile and link, the -lm belongs on the link command.

Last edited by johnsfine; 05-30-2013 at 08:37 AM.
 
2 members found this post helpful.
Old 05-30-2013, 08:38 AM   #4
islam_assi
LQ Newbie
 
Registered: May 2013
Location: Palestine
Posts: 6

Original Poster
Rep: Reputation: Disabled
how can I add (-lm) to the command ??
the command which I used to compile is (gcc -o exo2 exo2.c exo2_main.c)

Last edited by islam_assi; 05-30-2013 at 08:41 AM.
 
Old 05-30-2013, 08:40 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
What part of "add it to the command" did you miss? gcc -o exo2 exo2.c exo2_main.c -lm
 
2 members found this post helpful.
Old 05-30-2013, 08:43 AM   #6
islam_assi
LQ Newbie
 
Registered: May 2013
Location: Palestine
Posts: 6

Original Poster
Rep: Reputation: Disabled
the problem solved ,,,, thank you guys
 
  


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
[SOLVED] compiling error in sl-current "undefined reference to symbol"? marbangens Slackware 2 04-07-2013 09:51 AM
Error "Undefined reference" while compiling a c program in linux acharyaa Linux - General 4 07-25-2011 11:16 PM
[SOLVED] kernel compiling undefined reference error bahhti Programming 5 04-01-2010 11:00 PM
Error while compiling ecryptfs utils : undefined reference to `PK11_ImportSymKey' nitinarora Linux - Kernel 3 11-24-2009 04:21 AM
Several "undefined reference to" error when compiling a C programm acer_peri Programming 8 12-11-2008 05:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:54 AM.

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