problem with using sin,cos,log etc in c program in ubuntu7.04

Asked by dearprasanta

Having installed ubuntu7.04 from live cd i exucuted a c program. It could not compile c program telling that
stdio.h such directory not found.Then i installed libc6-dev.Then executed the same program which has only printf and scanf commands. and it executed.then after when i compiled a program with pow(x,2) it also excuted.but when i executed a program with sin ,cos, log functions it could not compile.but i had added math.h header file.it is telling that undefined reference to `sin'
collect2: ld returned 1 exit status
can u give me the solution for this?
                                                                thank u.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
dearprasanta
Solved:
Last query:
Last reply:
Revision history for this message
Simos Xenitellis  (simosx) said :
#1

You should install "build-essential", a meta-package that brings in all the required development packages.

Then, programs like

#include <math.h>

int main(void)
{
        float f = 10;

        sin(f);

        return 0;
}

should work like a charm (gcc sample.c -o sample).

Revision history for this message
dearprasanta (dearprasanta-gmail) said :
#2

as you told me to install the "build-essential" metapackage ,i did so but still then the same problem takes place.

Revision history for this message
Simos Xenitellis  (simosx) said :
#3

Can you post please
1) A minimal C program that does not compile on your computer? How about the program I put above? Does that work for you?
2) The command line you use to compile the program.
3) The full output from the compilation, any errors or warnings.

Revision history for this message
dearprasanta (dearprasanta-gmail) said :
#4

ok thank u sir for ur kind coperation. I am attaching these program as well as comands and outputs.
the program name is test1.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 main()
{float x,y;
printf("enter the value of x\n");
scanf("%f",&x);
 printf("Hello world\n");
y=sin(x);
printf("value of y is = %f \n",y);
 printf("\nEnd of program\n");
 }
i compiled by both cc and gcc. their outputs are as follows.
mplab95@mplab95-desktop:~$ cc test1.c
/tmp/ccGC5GZ9.o: In function `main':
test1.c:(.text+0x43): undefined reference to `sin'
collect2: ld returned 1 exit status
mplab95@mplab95-desktop:~$ gcc test1.c
/tmp/ccKLD7cp.o: In function `main':
test1.c:(.text+0x43): undefined reference to `sin'
collect2: ld returned 1 exit status
mplab95@mplab95-desktop:~$

Revision history for this message
Simos Xenitellis  (simosx) said :
#5

I do not have a compiler on this computer.

The error you get is that the linker cannot link in the mathematical functions in the final executable.
There are two things you can do,

1. Can you please try the following command line

gcc -o test1 test1.c

2. Can you please try

gcc -o test1 -lm test1.c

The "-lm" option means to link in the library with the mathematical functions.

For some reason on my system I do not need to put "-lm". Maybe because I use "gcc" instead of "cc" in order to run the compiler (it's the same program in both cases).

I hope this helps.

Revision history for this message
dearprasanta (dearprasanta-gmail) said :
#6

as u told i tried with
gcc -o test1 test1.c
still then the same problem takes place like
mplab95@mplab95-desktop:~$ gcc -o test1 test1.c
/tmp/ccwJvhS4.o: In function `main':
test1.c:(.text+0x43): undefined reference to `sin'
collect2: ld returned 1 exit status
mplab95@mplab95-desktop:~$

but when i tried with
gcc -o test1 -lm test1.c
then no error is shown like
mplab95@mplab95-desktop:~$ gcc -o test1 -lm test1.c
mplab95@mplab95-desktop:~$

but there is no output ie when i am executing
./a.out
it shows like this
no such file found.
can u please provide me the solution.
                                                                                   THank u.

Revision history for this message
Simos Xenitellis  (simosx) said :
#7

Hey, you just made it!

In the command

$ gcc -o test1 -lm test1.c

"-o test1" means to use the name "test1" for the compiled program. Your executable is "test1", not the default "a.out". If you did not specify a name with "-o", you would get the standard "a.out".

Therefore, run

./test1

Revision history for this message
dearprasanta (dearprasanta-gmail) said :
#8

thank you very much.u solved my problem.