Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problem in compiling c under gcc

Status
Not open for further replies.

u04f061

Advanced Member level 4
Full Member level 1
Joined
Mar 3, 2006
Messages
103
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Activity points
2,085
+undefined reference to log2 +lm

i am using ubuntu and gcc for c compiling. i have designed the following program,

#include <stdio.h>
#include <math.h>
int main(void){
float x=10.0;
float y=30.0;
printf("\n %f \t %f \t %1f",log(x),log2(x),sqrt(y));
return 0;
}

this is the command line result of gcc


ejaz@msiddique:~/cproj$ gcc -o math math.c
math.c: In function ‘main’:
math.c:6: warning: incompatible implicit declaration of built-in function ‘log2’/tmp/ccLMCrbz.o: In function `main':math.c:(.text+0x4d): undefined reference to `sqrt'
:math.c:(.text+0x61): undefined reference to `log2'
:math.c:(.text+0x6f): undefined reference to `log'
collect2: ld returned 1 exit status
ejaz@msiddique:~/cproj$


please help me in solving this trouble
 

You haven't included math library. Compile and link your program like this:

gcc -o math math.c -lm


and here is the output:

2.302585 3.321928 5.477226
 

    u04f061

    Points: 2
    Helpful Answer Positive Rating
cfant said:
You haven't included math library. Compile and link your program like this:

gcc -o math math.c -lm


and here is the output:

2.302585 3.321928 5.477226

o thats good,works fine thanks for reply.but still it is giving me some warning like this

ejaz@msiddique:~/cproj$ gcc -o math math.c -lm
math.c: In function ‘main’:
math.c:6: warning: incompatible implicit declaration of built-in function ‘log2’ejaz@msiddique:~/cproj$ ./math

2.302585 3.321928 5.477226ejaz@msiddique:~/cproj$

ok also i want some explanation about this.

does math library is not in standered c library ? for which of header files we need to link them? does -lm command is sufficient to link any library?
thanx for your nice guide

Added after 1 hours 6 minutes:

also there is problem with double data types.look at the program


// programe to calculate income tax

#include<stdio.h>

int main(void){
const double TAX_RATE = 0.025;
double grossIncome ,totalTax;
printf("Enter the value of income in rupeese");
scanf("%f",&grossIncome);
totalTax = grossIncome * TAX_RATE;
printf("total tax is %f \n",totalTax);
return 0;
}

here is the output

ejaz@msiddique:~/cproj$ gcc -o tax tax.c
ejaz@msiddique:~/cproj$ ./tax
Enter the value of income in rupeese100.0
total tax is -0.000000
ejaz@msiddique:~/cproj$
 

Check your scanf function. You have declared f to be double, but you are using float. If you change it to:

scanf("%lf",&grossIncome);


program should run ok. As for math library -lm will link only math library.

Added after 6 minutes:

Which version of the gcc you are using? I don't see that warning. Mine is:

gcc version 3.4.3
 

    u04f061

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top