LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   collect2: ld returned 1 exit status (http://www.linuxquestions.org/questions/linux-software-2/collect2-ld-returned-1-exit-status-759984/)

kvijaik 10-06-2009 02:26 AM

collect2: ld returned 1 exit status
 
#include <stdio.h>
#include <math.h>
main()
{
float x, y, z, d = 0;
printf("Please enter value for x:\n");
scanf("%f", &x);
printf("Please enter value for y:\n");
scanf("%f", &y);
printf("Please enter value for z:\n");
scanf("%f", &z);
d = sqrt((x*x) + (y*y) + (z*z));
printf("The distance to your point was %f\n", d);
}

-bash-3.2# vi pythogoros.c
-bash-3.2# cc -o pythogoros pythogoros.c
/tmp/cc31sDdi.o: In function `main':
pythogoros.c:(.text+0xb0): undefined reference to `sqrt'
collect2: ld returned 1 exit status

I have typed the above program in vi editor and saved and compiled as mentioned above i am getting error message:
/tmp/cc31sDdi.o: In function `main':
pythogoros.c:(.text+0xb0): undefined reference to `sqrt'
collect2: ld returned 1 exit status

will somebody help me on this. I am running the Fedora 10 OS.

lutusp 10-06-2009 03:48 AM

Quote:

Originally Posted by kvijaik (Post 3709219)
#include <stdio.h>
#include <math.h>
main()
{
float x, y, z, d = 0;
printf("Please enter value for x:\n");
scanf("%f", &x);
printf("Please enter value for y:\n");
scanf("%f", &y);
printf("Please enter value for z:\n");
scanf("%f", &z);
d = sqrt((x*x) + (y*y) + (z*z));
printf("The distance to your point was %f\n", d);
}

-bash-3.2# vi pythogoros.c
-bash-3.2# cc -o pythogoros pythogoros.c
/tmp/cc31sDdi.o: In function `main':
pythogoros.c:(.text+0xb0): undefined reference to `sqrt'
collect2: ld returned 1 exit status

I have typed the above program in vi editor and saved and compiled as mentioned above i am getting error message:
/tmp/cc31sDdi.o: In function `main':
pythogoros.c:(.text+0xb0): undefined reference to `sqrt'
collect2: ld returned 1 exit status

will somebody help me on this. I am running the Fedora 10 OS.

You are invoking the compiler incorrectly. Try this:

Code:

$ g++ pythogoros.c -o pythogoros
Also, you need to start indenting your source code.

And ... are you obliged to use C? A C++ version of this same program would be much more robust and understandable. And it wouldn't need to use the risky and unreliable scanf() function.

Finally, there are much better editors than vi. Using vi is going to hurt your productivity.

kvijaik 10-07-2009 09:17 PM

Hi Paul,

Thanks for the help it worked for me.

I am a learner of C language on linux, quick question why the code was not getting compiled with the cc and gcc. If i want to compile the code with cc what will be my command.

Thanks,
Vijai.K


All times are GMT -5. The time now is 02:36 AM.