funny problem in C program !

S

Suri

hi im trying to run this simple program. any help shall be appreciated
..pls see error message.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
double pic;
pic= 4.0 * atan(1.0);
printf("pi is = %f \n",pic);
}
error when i try to compile ...

[harshit@localhost ~]$ gcc harshit.c
/tmp/ccRf4QzM.o(.text+0x1b): In function `main':
: undefined reference to `atan'
collect2: ld returned 1 exit status
 
M

Morris Dovey

Suri said:
hi im trying to run this simple program. any help shall be appreciated
..pls see error message.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
double pic;
pic= 4.0 * atan(1.0);
printf("pi is = %f \n",pic);
}
error when i try to compile ...

[harshit@localhost ~]$ gcc harshit.c
/tmp/ccRf4QzM.o(.text+0x1b): In function `main':
: undefined reference to `atan'
collect2: ld returned 1 exit status
^^^^^^^^^^^^^^^^^^^^^^^^^

Suri...

Not a compile problem. ld can't resolve atan() - try:

gcc -lm harshit.c

so that ld will know to search the math library.

--> See FAQ: http://www.eskimo.com/~scs/C-faq/q14.3.html
 
B

Bruno Desthuilliers

Suri said:
hi im trying to run this simple program. any help shall be appreciated
.pls see error message.


I can see 2 C-related problems :
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main()

should be int main(void) or int main(int argc, char **argv)
{
double pic;
pic= 4.0 * atan(1.0);
printf("pi is = %f \n",pic);

main must return an int. Default is EXIT_SUCCESS (defined in stdlib.h).
}
error when i try to compile ...

The compiler should at least warn you that it reaches the end of a
non-void function. If it doesn't, RTM and learn how to raise warning level.

[harshit@localhost ~]$ gcc harshit.c
/tmp/ccRf4QzM.o(.text+0x1b): In function `main':
: undefined reference to `atan'
collect2: ld returned 1 exit status

This is not a problem with the C language, it's a problem with your
linker. And it's a FAQ. RTF !-)

Bruno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top