/tmp/ccjiJKv2.o: In function func':
b.c:(.text+0x16b): undefined reference tosqrt'
collect2: ld returned 1 exit status
Here is the code and I am using gcc compiler..
/*Write a function that receive 5 integers and returns the sum,average and standard deviation of these numbers*/
/*Author:Udit Gupta Date:10/08/2011*/
#include<stdio.h>
#include<math.h>
void func (int *,float *,float *);
int main () {
float avg,std_dev;
int sum;
func (&sum,&avg,&std_dev); /*Passing address of variables where output will be stored.....*/
printf ("The sum of numbers is %d\n",sum);
printf ("The average of numbers is %f\n",avg);
printf ("The standard deviation of numbers is %f\n",std_dev);
}
void func (int *sum_, float *avg_ , float * std_dev_) {
int n1,n2,n3,n4,n5;
printf("Please enter the number:");
scanf("%d%d%d%d%d",&n1,&n2,&n3,&n4,&n5);
/*Formula for sum,average and standard deviation*/
*sum_ = n1+n2+n3+n4+n5; /*Writing output at the address specified by arguments of function*/
*avg_ = *sum_ / 5 ;
*std_dev_ = sqrt ( pow((n1-*avg_),2)+pow((n2-*avg_),2)+pow ((n3-*avg_),2)+pow ((n4-*avg_),2)+pow ((n5-*avg_),2)/4) ;
}
You are not linking the library which contains the implementation of sqrt.
That library is known as "libm" (the math library), and can be linked to as follows:
gcc -o myprog infile.c -lm
#include <math.h> - is this what you need?
Also, Link with -lm
you have to link the math library. -lm
Related
This question already has answers here:
Undefined reference to sqrt (or other mathematical functions)
(5 answers)
Closed 5 years ago.
So I'm trying to make a program that calculates the quadratic formula, but when I try to compile the code, I get the following:"undefined reference to sqrt"
But I tried defining sqrt via math.h and 2 other times in the code.
I have attached my code
Any help would be greatly appreciated
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double sqrt(double);
int main (void) {
double sqrt(double);
int a,b,c;
double discriminant,squarerootofdis,root1, root2;
printf("Please enter the coefficient of x^2:");
scanf("%d",&a);
printf("Please enter the coefficient of x:");
scanf("%d",&b);
printf("Please enter the integer value of the ploynomial:");
scanf("%d",&c);
if (a==0 && b==0)
{printf("This case is extremely degenerate");}
else if (a==0 && b!=0)
{root1=-c/b;
printf("Degenerate one real root: %lf\n",root1);}
else{
discriminant = ((b*b)-(4*a*c));
squarerootofdis = sqrt(discriminant);
root1 = (squarerootofdis-b)/(2*a);
root2 = (-squarerootofdis-b)/(2*a);
if (discriminant>0)
printf("Two real roots: %lf\n %lf\n", root1, root2);
else if (discriminant == 0)
printf("Degenerate one real root: %lf\n",root1);
else if (discriminant<0)
printf("Two complex roots: %lf\n %lf\n", root1, root2);
}
}
To use the sqrt function (or any function defined in math.h), you'll have to link the m library:
~$ gcc -lm yourcode.c -o program
Did you compile with -lm linked?
Header file will provide the decalration to the sqrt() function. To have the definition, you need to link with the math library consisting of the function definition.
Example:
gcc test.c -o output -lm
Please use the below command
gcc test.c -lmath
I have a problem with my function waveres. It is supposed to return a amplitude as a float, but does not. It return a random high number. I think it is the definition in my header file that is not "seen" by the main function. The other functions does work so I did not include them. When the waveres function runs, it prints correct values of amp.
Header file
#include <stdio.h>
#include <math.h>
#include <time.h> /* til random funksjonen */
#include <stdlib.h>
void faseforskyvning(float epsi[]);
float waveres(float S[],float w[],float *x, float *t, float epsi[]);
void lespar(float S[], float w[]);
Main program
#include "sim.h"
main()
{
float epsi[9], t = 1.0, x = 1.0;
float S[9], w[9];
float amp;
faseforskyvning(epsi);
lespar(S,w);
amp=waveres(S,w,&x,&t,epsi);
printf("%f\n", amp);
}
waveres:
#include "sim.h"
float waveres(float S[],float w[],float *x, float *t, float epsi[])
{
float amp = 0, k;
int i;
for(i=0;i<10;i++)
{
k = pow(w[i],2)/9.81;
amp = amp + sqrt(2*S[i]*0.2)*cos(w[i]*(*t)+k*(*x)+epsi[i]);
printf("%f\n",amp);
}
return(amp);
}
Sample output where the two last number are supposed to be the same.
0.000000
0.261871
3.750682
3.784552
3.741382
3.532950
3.759173
3.734213
3.418669
3.237864
1078933760.000000
A source to the error might be me compiling wrong. Here is a output from compiler:
make
gcc -c -o test.o test.c
gcc -c -o faseforskyvning.o faseforskyvning.c
gcc -c -o waveres.o waveres.c
gcc -c -o lespar.o lespar.c
gcc test.o faseforskyvning.o waveres.o lespar.o -o test -lm -E
gcc: warning: test.o: linker input file unused because linking not done
gcc: warning: faseforskyvning.o: linker input file unused because linking not done
gcc: warning: waveres.o: linker input file unused because linking not done
gcc: warning: lespar.o: linker input file unused because linking not done
You have undefined behavior, you iterate untill 10
for(i=0;i<10;i++)
But your arrays has size 9 which means the biggest index is 8
float epsi[9], t = 1.0, x = 1.0;
float S[9], w[9];
You need to change your loop to
for(i=0;i<9;i++)
Also your arrays are not initialized, this is also provokes undefined behavior. For example
float w[9]={0};
initializes all elements of array w with 0
I'm an absolute beginner to C and I've read a few books but never really played with it. I'm starting to try to apply what I've read with a very simple program that returns the sin of a number. The hardest thing I've encountered with C is knowing how and when to use pointers.
I'm sure this is simple but here is how I've written my test:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x;
printf("Enter a number to calculate the sin(x): \n");
scanf("%lf", &x);
printf("sin(%lf) = %lf\n", x, sin(x));
return 0;
}
I'm compiling and executing this code in Ubuntu
gcc -lm sinCalc.c && ./a.out
Error I'm receiving is this:
/tmp/blaha.o: In function `main':
sinCalc.c:(.text+0x31): undefined reference to `sin'
collect2: ld returned 1 exit status
Undefined symbols are resolved left to right, so
gcc sinCalc.c -lm && ./a.out
should work.
Are they [structs] like an interface in Java?
No. Structs are an aggregate of a number (1 or more) of types that can be dealt with as a single unit in certain circumstances (assignment, parameter passing).
I am trying to write a program to approximate pi. It basically takes random points between 0.00 and 1.00 and compares them to the bound of a circle, and the ratio of points inside the circle to total points should approach pi (A very quick explanation, the specification goes in depth much more).
However, I am getting the following error when compiling with gcc:
Undefined first referenced
symbol in file
pow /var/tmp//cc6gSbfE.o
ld: fatal: symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
What is happening with this? I've never seen this error before, and I don't know why it's coming up. Here is my code (though I haven't fully tested it since I can't get past the error):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
float x, y;
float coordSquared;
float coordRoot;
float ratio;
int n;
int count;
int i;
printf("Enter number of points: ");
scanf("%d", &n);
srand(time(0));
for (i = 0; i < n; i++) {
x = rand();
y = rand();
coordSquared = pow(x, 2) + pow(y, 2);
coordRoot = pow(coordSquared, 0.5);
if ((x < coordRoot) && (y < coordRoot)) {
count++;
}
}
ratio = count / n;
ratio = ratio * 4;
printf("Pi is approximately %f", ratio);
return 0;
}
use -lm during compilation(or linking) to include math library.
Like this: gcc yourFile.c -o yourfile -lm
need to Link with -lm.
gcc test.c -o test -lm
The error is produced by the linker, ld. It is telling you that the symbol pow cannot be found (is undefined in all the object files handled by the linker). The solution is to include the library which includes the implementation of the pow() function, libm (m for math). [1] Add the -lm switch to your compiler command line invocation (after all the source file specifications) to do so, e.g.
gcc -o a.out source.c -lm
[1] Alternatively, you could have your own implementation of pow() in a separate translation unit or a library, but you would still have to tell the compiler/linker where to find it.
I'm don't seem to be able to generate random number in C under Ubuntu 12.04.
I wrote the fallowing code:
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main (int argc,char* argv[])
{
int number;
clear();
number = rand() % 2; // want to get only 0 or 1
printf("%d",number);
getch();
return 0;
}
I named the file "test_gcc.c".
After that I compile it with:
$ sudo gcc -o test_gcc test_gcc.c
And i get the following message:
/tmp/ccT0s12v.o: In function `main':
test_gcc.c:(.text+0xa): undefined reference to `stdscr'
test_gcc.c:(.text+0x12): undefined reference to `wclear'
test_gcc.c:(.text+0x44): undefined reference to `stdscr'
test_gcc.c:(.text+0x4c): undefined reference to `wgetch'
collect2: ld returned 1 exit status
Can somebody tell me what did I do wrong?
And also how to generate random number in C on Ubuntu 12.04 using gcc?
Thanks in advance!
This has nothing to do with random numbers. The problem is that you're linking without the curses library.
You need to add -lncurses to your gcc command line:
$ gcc -o test_file test_file.c -lncurses
You didn't seed the random number generator. <-- Not the reason for errors
Use srand(time(0)); before calling rand().
Use srand ( time(NULL) ); before number = rand() % 2; to get different random number every time the executable is ran.
For errors:
remove clear() and use getchar() instead of getch() and then it
should worked fine.
getch() is used in compilers that support un-buffered input, but in
case of gcc it's buffered input so use getchar().
code:
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main (int argc,char* argv[])
{
int number;
srand(time(NULL));
number = rand() % 2; // want to get only 0 or 1
printf("%d",number);
getchar();
return 0;
}
Try :
gcc -o test_gcc test_gcc.c -lncurses