By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,302 Members | 3,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,302 IT Pros & Developers. It's quick & easy.

funny problem in C program !

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Suri wrote:
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

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall very far from the tree.

Nov 13 '05 #2

P: n/a
Suri <hs***@usc.edu> writes:
Subject: funny problem in C program !


Yeah, it's so funny there's a FAQ for it. Please read the FAQ.
Nov 13 '05 #3

P: n/a
Suri wrote:
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.

<ot> for gcc, -Wall -ansi -pedantic should be a good start </ot>
[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

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.