LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-29-2009, 09:50 PM   #1
kvijaik
LQ Newbie
 
Registered: Sep 2009
Posts: 9

Rep: Reputation: 0
How to get rid of this most often facing issue of collect2: ld returned 1 exit status


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
double x, y, z;
printf ("Enter the value of x:\n");
scanf ("%f",&x);
printf ("Enter the value of y:\n");
scanf ("%f", &y);
printf("Enter the value of z:\n");
scanf ("%f", &z);
func(x, y, z);
}

func(double a, double b, double c)
{
double d;
d = sqrt ((a*a) + (b*b) + (c*c));
printf("The distance is: %f\n", d);
}

i compiled the above program with the command:

gcc pythogoros.c -o pythogoros

It gave the compiler error shown below:

/tmp/ccg6Jmch.o: In function `func':
pythogoros.c.text+0xf0): undefined reference to `sqrt'
collect2: ld returned 1 exit status

I need help for the above mentioned issue.
 
Old 10-30-2009, 01:28 AM   #2
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Porteus atma
Posts: 84

Rep: Reputation: 21
Use -lm with gcc

Hi
You should use -lm with gcc command while compiling.
Code:
gcc -lm pythogorus.c -o pythogorus
Another important thing is that you should use %lf not %f for double variables.
So your modified code will look as below.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
double x, y, z;
printf ("Enter the value of x:\n");
scanf ("%lf",&x);
printf ("Enter the value of y:\n");
scanf ("%lf", &y);
printf("Enter the value of z:\n");
scanf ("%lf", &z);
func(x, y, z);
}

func(double a, double b, double c)
{
double d;
d = sqrt ((a*a) + (b*b) + (c*c));
printf("The distance is: %lf\n", d);
}
Notice that the only change is to replace %f with %lf.

All the best
-indiajoe

Last edited by indiajoe; 10-30-2009 at 01:29 AM.
 
Old 11-01-2009, 03:54 AM   #3
kvijaik
LQ Newbie
 
Registered: Sep 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Hi Indiajoe.

The solution given by you worked for me.

Thanks a Ton.

Regards,
Vijai.K
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
collect2: ld returned 1 exit status kvijaik Linux - Software 2 10-07-2009 10:17 PM
collect2: ld returned 1 exit status Ritendra Linux - Newbie 2 07-30-2009 08:39 AM
ld: cannot find -lc collect2: ld returned 1 exit status ArbuZz Linux From Scratch 3 09-27-2005 01:52 AM
collect2: ld returned 1 exit status abirami Linux - Software 0 03-02-2005 02:07 AM
popa3d collect2: ld returned 1 exit status datapunk Linux - Software 1 01-02-2004 07:48 PM


All times are GMT -5. The time now is 12:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration