navi C $ cc myvector.c
/tmp/cc4W8xkO.o: In function `vectdlina':
myvector.c:(.text+0x108): undefined reference to `sqrt'
collect2: выполнение ld завершилось с кодом возврата 1
самое интересное, что в K&R, в самом начале главы о структурах, есть аналогичный код:
navi C $ cc point.c
/tmp/ccBEtljl.o: In function `main':
point.c:(.text+0x6e): undefined reference to `sqrt'
collect2: выполнение ld завершилось с кодом возврата 1
#include <stdio.h>
#include <math.h>
void main( void );
float geta ( double );
float getb ( double );
float getsee ( void );
float g( double );
double width = 0.0001;
double rightorleft=0;
int numterms=10;
double T=1;
double f=1;
void main( void) {
double a[ numterms + 1], b[ numterms + 1], c, ctoo, n;
int i, j;
printf( "\n" );
c = getsee( );
for ( n=1; n <= numterms; n++ ) {
i = n;
a[ i ] = geta( n );
}
for ( n=1; n <= numterms; n++ ) {
i = n;
b[ i ] = getb( n );
}
rightorleft = width;
ctoo = getsee();
for ( i = 1; i <= numterms; i++ ) {
printf( "%s%d%s", "a", i, " is: " );
printf( "%lf", a[ i ]);
printf( "%s%d%s", " b", i , " is: ");
printf( "%lf\n", b[ i ]);
}
printf( "\n%s%lf\n", "c is ", c );
printf( "%s%lf\n\n", "ctoo is ", ctoo );
}
float geta( double n ) {
double i;
float total = 0;
double end;
if (rightorleft==0) end = T - width;
else end = T;
for ( i = rightorleft; i <= end; i+=width)
total += width * ( g( i ) * cos(6.28 * n * f * i ) );
total *= 2/T;
return total;
}
float getb( double n ) {
double i;
float total = 0;
double end;
if (rightorleft == 0 ) end = T - width;
else end = T;
for ( i = rightorleft; i<= end; i+=width)
total += width * ( g( i ) * sin(6.28 * n * f * i ) );
total *= 2/T;
return total;
}
float getsee(void) {
double i;
float total = 0;
double end;
if (rightorleft == 0 ) end = T - width;
else end = T;
for ( i = rightorleft; i <= end; i+= width )
total += width * g( i );
total *= 2/T;
return total;
}
float g( double t ) {
return sqrt(1/( 1 + t ));
}
> gcc fourier1.c
fourier1.c: In function ‘main’:
fourier1.c:13: warning: return type of ‘main’ is not ‘int’
/tmp/ccl5MK1w.o: In function `geta':
fourier1.c:(.text+0x39c): undefined reference to `cos'
/tmp/ccl5MK1w.o: In function `getb':
fourier1.c:(.text+0x471): undefined reference to `sin'
/tmp/ccl5MK1w.o: In function `g':
fourier1.c:(.text+0x5a2): undefined reference to `sqrt'
collect2: ld returned 1 exit status
>
agent@notebook:~> gcc -o tm tm.c -lm
agent@notebook:~> ./tm
a1 is: 0.014061 b1 is: 0.089179
a2 is: 0.003285 b2 is: 0.046068
a3 is: 0.001108 b3 is: 0.030927
a4 is: 0.000328 b4 is: 0.023255
a5 is: -0.000036 b5 is: 0.018628
a6 is: -0.000234 b6 is: 0.015536
a7 is: -0.000354 b7 is: 0.013325
a8 is: -0.000432 b8 is: 0.011664
a9 is: -0.000486 b9 is: 0.010373
a10 is: -0.000524 b10 is: 0.009339
c is 1.656883
ctoo is 1.656825
agent@notebook:~>
и да, main() по хорошему int возвращает, хоть это и не критично в данном случае.
1) Компилите с -lm и с Sqrt вместо sqrt
2) Указывают на ошибку с S-s
3) Компилите с sqrt но _без_ -lm - в результате получаете ошибку автора темы
4) Агент подсказывает нужное направление наводящим вопросом в своём посте, однако вы его не видите.
Сделайте всё правильно и выложите результат - не мучьте себя и комьюнити и дайте себя спасти (:
1) Компилите с -lm и с Sqrt вместо sqrt
2) Указывают на ошибку с S-s
3) Компилите с sqrt но _без_ -lm - в результате получаете ошибку автора темы
4) Агент подсказывает нужное направление наводящим вопросом в своём посте, однако вы его не видите.
Сделайте всё правильно и выложите результат - не мучьте себя и комьюнити и дайте себя спасти (:
Вот сейчас откомпилю с sqrt с маленькой буковки, и с -lm, и покажу результат
Может быть я уже пал настолько что спасти меня невозможно )
>gcc fourier1.c -lm -o four
fourier1.c: In function ‘main’:
fourier1.c:13: warning: return type of ‘main’ is not ‘int’
> ./four
a1 is: 0.014061 b1 is: 0.089179
a2 is: 0.003285 b2 is: 0.046068
a3 is: 0.001108 b3 is: 0.030927
a4 is: 0.000328 b4 is: 0.023255
a5 is: -0.000036 b5 is: 0.018628
a6 is: -0.000234 b6 is: 0.015536
a7 is: -0.000354 b7 is: 0.013325
a8 is: -0.000432 b8 is: 0.011664
a9 is: -0.000486 b9 is: 0.010373
a10 is: -0.000524 b10 is: 0.009339
c is 1.656883
ctoo is 1.656825
>