C Tutorial
Why this undefined reference to pow error raised in compilation?
When we use the pow function or any other math functions from math header in c program on linux environment (RHEL, Fedora or CentOS), need to use -lm option for compilation.
otherwise you will get undefined reference to pow error, when you compile below program with no -lm option.
#include<stdio.h> #include<math.h> float PMT (int interest, int loan_amount, int total_months) { float rate, denominator; rate = (float) interest/100/12; denominator = pow((1+rate), total_months)-1; return (rate+(rate/denominator))*loan_amount; } void main() { int loan_amount, loan_term_years, num_of_emi_per_year; float rate_of_interest, pmt_value; printf("\nEnter loan amount: "); scanf("%d", &loan_amount); printf("\nEnter loan tearm in years: "); scanf("%d", &loan_term_years); printf("\nEnter number of EMI per year: "); scanf("%d", &num_of_emi_per_year); printf("\nEnter rate of interest: "); scanf("%f", &rate_of_interest); pmt_value= PMT(rate_of_interest, loan_amount, loan_term_years*num_of_emi_per_year); printf("\nEMI: %f", pmt_value); }
Output:
$ cc emi_calculator.c /tmp/cc4v5U2R.o: In function `PMT': emi_calculator.c:(.text+0x5a): undefined reference to `pow' collect2: error: ld returned 1 exit status
To fix this issue, need to use -lm option in compilation command.
Output:
$ cc emi_calculator.c -lm $ ./a.out Enter loan amount: 396000 Enter loan tearm in years: 4 Enter number of EMI per year: 12 Enter rate of interest: 15 EMI: 11020.946289
C program provides expected output when running a.out file once you compiled the program with -lm option.
If you want different name instead of a.out file, we need to use -o option to create executable file with custom name.
$ cc -o emi emi_calculator.c -lm $ ./emi Enter loan amount: 396000 Enter loan tearm in years: 4 Enter number of EMI per year: 12 Enter rate of interest: 15 EMI: 11020.946289
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page