LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-22-2021, 05:02 PM   #1
williambrown615
LQ Newbie
 
Registered: Jan 2014
Posts: 9

Rep: Reputation: Disabled
C Program error : undefined reference to pow


I typed a C program from the Internet that calculates Compound Interest.
One of the lines in the program reads CIFuture = PAmount*(pow((1+ROI/100), Time_Period));

When I run the program it gives the error: undefined reference to pow.

My gcc version is 7.31 20180712 (RED HAT 7.31-6.

I also included <stdio.h> and <math.h> in the program.

Why is pow undefined?
 
Old 07-22-2021, 05:09 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,118

Rep: Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687Reputation: 4687
Posting a) your full actual code and b) a link to where you found it on "The Internet" would be good first steps.

Also, it's interesting that you said you get that error when you run the program. How are you running it and how are you compiling it?
 
Old 07-22-2021, 05:20 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,816

Rep: Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252
I assume by program the OP is referring to gcc.

You need to specify the math library on the command line ie.

gcc -o my_program my_program.c -lm
 
4 members found this post helpful.
Old 07-23-2021, 07:47 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Summer Midwest USA, Central Illinois, Winter Central Florida
Distribution: SlackwareŽ
Posts: 13,731
Blog Entries: 37

Rep: Reputation: 2916Reputation: 2916Reputation: 2916Reputation: 2916Reputation: 2916Reputation: 2916Reputation: 2916Reputation: 2916Reputation: 2916Reputation: 2916Reputation: 2916
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 07-23-2021, 08:34 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,205
Blog Entries: 1

Rep: Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565Reputation: 1565
Well, there was a change in the linking, earlier this worked:
Code:
gcc -g -W -Wall -lm -o mytest mytest.c
now it is:
Code:
gcc -g -W -Wall -o mytest mytest.c -lm
 
Old 07-23-2021, 12:29 PM   #6
williambrown615
LQ Newbie
 
Registered: Jan 2014
Posts: 9

Original Poster
Rep: Reputation: Disabled
undefinded referencwe to pow

/* C Program to Calculate Compound Interest */

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

int main()
{
float PAmount, ROI, Time_Period, CIFuture, CI;

printf("\nPlease enter the Principal Amount : \n");
scanf("%f", &PAmount);

printf("Please Enter Rate Of Interest : \n");
scanf("%f", &ROI);

printf("Please Enter the Time Period in Years : \n");
scanf("%f", &Time_Period);

CIFuture = PAmount * (pow(( 1 + ROI/100), Time_Period));
CI = CIFuture - PAmount;

printf("\nFuture Compound Interest for Principal Amount %.2f is = %.2f", PAmount, CIFuture);
printf("\nCompound Interest for Principal Amount %.2f is = %.2f", PAmount, CI);

return 0;
} This is the program I run and get an error message: undefined reference to pow. Shouldn't pow be included in the math library which I include in my program?
 
Old 07-23-2021, 12:59 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,716
Blog Entries: 11

Rep: Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747
Welcome to the Programming forum!

Please wrap your code and data snippets inside [CODE]...[/CODE] tags. Doing so will preserve indentation and provide other visual clues which make it easier for others to comprehend. You may write those yourself as shown, or use the # button available with Advanced edit options. (A complete list of BBCode tags is always available via a link near the bottom of every thread view).

As others have already pointed out, you need to link to the math shared libraries. I'll try to elaborate what has already been said...

When you say that you "run" the program what you are actually talking about is running the C language compiler, gcc. C programs must first be compiled and linked, then you run the resultant executable program, often called a "binary".

As others have asked, it would be helpful if you would show the actual command that you are running, but it is probably something like this (using myprog.c as your source code filename):

Code:
gcc -o myprog myprog.c
The compiler will build object files which must then be linked in order to make use of the shared library files on your system, the math library in this case, and it is the linker which is producing the error message you see because it is not being told which library to link. You can do that in your command to gcc with the -l option (lower case L):

Code:
gcc -o myprog myprog.c -lmath

OR simply

gcc -o myprog myprog.c -lm
That will produce a file named myprog which is your executable program.

You may then run that program like this:

Code:
./myprog
... where myprog is used as the example filename.

Try this out and let us know if you have any further problems! If so, please show us the exact commands you are using and the error messages they produce.

Good luck!
 
2 members found this post helpful.
Old 07-24-2021, 11:52 AM   #8
williambrown615
LQ Newbie
 
Registered: Jan 2014
Posts: 9

Original Poster
Rep: Reputation: Disabled
undefined reference to pow

I already have the gcc compiler installed on my Linux machine. To compile the the program I type cc file name .c If it compiles with no errors I type ./a.out to run the program. In my code I have #include <stdio.h> and #include <math.h> The pow function that deals with powers should be in the math library already. When I compile the program I get the error undefined reference to pow. Why is that?
 
Old 07-24-2021, 12:01 PM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,550

Rep: Reputation: Disabled
The 8th line in the pow(3) manual page:
Quote:
Link with -lm.
 
Old 07-24-2021, 01:04 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,816

Rep: Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252Reputation: 4252
Quote:
When I compile the program I get the error undefined reference to pow. Why is that?
Even though you have #include <math.h> included in your program the actual code that executes that function is in a separate binary file called a library. The pow function is unknown without that library and one way to include it is to explicitly add it as a command line option. The -lm tells the compiler to include the math module when linking your program. The gcc compiler actually does several things that eventually result in an executable program that are not really explained well if at all in basic howtos.

There have been at least 4 posts that have told you to use -lm ...

Last edited by michaelk; 07-24-2021 at 01:08 PM.
 
Old 07-24-2021, 01:05 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,716
Blog Entries: 11

Rep: Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747
Quote:
Originally Posted by williambrown615 View Post
The pow function that deals with powers should be in the math library already. When I compile the program I get the error undefined reference to pow. Why is that?
Please read and try to understand the information already posted.

The compiler is indeed using the math.h header when it compiles the object file.

The linker, which is invoked by the compiler and which must connect, or link the object files to the system's precompiled shared library files does not use the math.h header and must be told where to find those functions provided by the math library. You do that with the -lm option.

Please at least try to compile with the -lm option on your cc command line...

Code:
cc filename.c -lm
./a.out
Does that work? Do you see why?
 
1 members found this post helpful.
Old 07-25-2021, 12:13 PM   #12
williambrown615
LQ Newbie
 
Registered: Jan 2014
Posts: 9

Original Poster
Rep: Reputation: Disabled
pow undefined reference problem solved. A big thanks to all who have helped.
Linux Question is Awesome
 
Old 07-25-2021, 01:30 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,716
Blog Entries: 11

Rep: Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747
Good to know!

If your question has been answered please use the Thread Tools dialog at top of your thread to mark it as SOLVED. Thanks!
 
Old 07-25-2021, 10:01 PM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,291
Blog Entries: 4

Rep: Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318Reputation: 3318
FYI, there are two ways that your program can be "linked" with the other bits of executable code that it needs: static linking (as has been done here) and dynamic linking. Here is a link to a thorough article which explains this topic.
 
Old 07-30-2021, 12:46 PM   #15
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,338
Blog Entries: 29

Rep: Reputation: 163Reputation: 163
Quote:
Originally Posted by williambrown615 View Post
I already have the gcc compiler installed on my Linux machine. To compile the the program I type cc file name .c If it compiles with no errors I type ./a.out to run the program. In my code I have #include <stdio.h> and #include <math.h> The pow function that deals with powers should be in the math library already. When I compile the program I get the error undefined reference to pow. Why is that?
simply, your code is calling function pow() and you did not include function pow(). if you had written function pow() in your code, that would have included it. but you probably intended to call the math library function of the same name. others have already described how to do that.
 
  


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
error (undefined reference to `pow') while compiling a C code in terminal islam_assi Linux - Newbie 5 05-30-2013 08:43 AM
C program - Undefined reference error Astrodude Programming 3 07-23-2010 01:20 PM
need a program of c made in linux to calculate a raised to the power b using pow fun mona_iitr Programming 5 03-28-2007 08:56 AM
Simple C pow() program, strange error funkymunky Programming 2 06-25-2004 04:20 AM
C programming: pow(0,0) = 1 ? Should be undefined. the theorist Programming 10 04-08-2003 06:29 PM

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

All times are GMT -5. The time now is 04:13 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