ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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):
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.
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?
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 ...
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...
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.